Solved

tricky or not

Posted on 2001-06-14
11
221 Views
Last Modified: 2010-04-02
ok guys,
i m not sure it can be done or not, but lets try :-)

i want to send a class name (not object ) to a function

class A
{
};
void Show(SOMETYPE a)
{
  SOMETYPE a = new SOMETYPE;
}

can i do that ??? if so...how ??
shay
0
Comment
Question by:Shay050799
11 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 6191020
No.

The veriable you are passing is not a pointer.

What exactly do you want to do, what is your goal?
0
 
LVL 5

Expert Comment

by:proskig
ID: 6191036
I guess you need to implement kind of class factory. Unfortunately there is no portable way to do that, but anyway, you can try to do the following: refer to Andrei Alexandrescu book Modern C++ Design, or/and use typeid and name().

E.g.
void g(Component* p)
{
 cout << typeid(*p).name();
}

0
 
LVL 7

Accepted Solution

by:
KangaRoo earned 50 total points
ID: 6191582
Compile-time:

template<typename T>
T* make() {return new T;}

struct A {}
A* a = make<A>();

For runtime type determinatin there may be other solutions, depending on how you want it to be used.
0
 

Expert Comment

by:NickAtBOM
ID: 6191587
Do you know the 'type' at compile time?  If so, the best way to do this is to use templates:

tempalate <class T>
void Show()
{
   T* pT = new T;     // warning, memory leak here
}


then to use
main()
{
   Show<A>();    // where A is the class you want to 'show'
}
0
 

Expert Comment

by:NickAtBOM
ID: 6191600
KangaRoo - you got that in a whole min earlier...
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 3

Expert Comment

by:elcapitan
ID: 6193971
From a different angle, more OOP. You can use virtual functions to create virtual "constructor". for example, lets say we have 3 classes:

class A
{
  //virtual methods
  virtual A *Make_obj(){return new A();}
};

class B: public A
{
  //methods
  virtual A *Make_obj(){return new B();}
};

class C: public A
{
  //methods
  virtual A *Make_obj(return new C();)
};

and in the main:

void main()
{
  //1. we have an array with 3 pointers to objects
  A *arr[3];
  arr[0]=new B();
  arr[1]=new C();
  arr[2]=new B();
 
  //2. we want to fill the second array with objects of
  //   the same type.
  A *arr1[3];
  for(int i=0;i<3;++i)
  {
    arr1[i]=arr[i]->Make_obj();
  }
}

--EC--
0
 
LVL 30

Expert Comment

by:Axter
ID: 6193994
elcapitan,
The questioner asked for a C method.
0
 
LVL 30

Expert Comment

by:Axter
ID: 6193996
elcapitan,
Disregard my previous comment.  I got my questions confused.
Sorry
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6799463
Shay----->  You have asked 95 questions at this site, and only awarded 53 of them....  this is totally against the Guidelines of this site.  Please return to them and update/finalize them this week.  The list is below.  Of these 53 awarded, there are 7 questions that are -1 user id problems which we will need to manually correct.  Please accept the first correct response in each of them and award the experts who helped you with the deserved "A" grade, or comments if less than an A as to why.  If you need our help to special handle any of these, let us know that as well via comment here.  If these are not finalized this week, I will contact Administration for further action.  Please click the HELP DESK link on the left for the rules of engagement here (Guidelines, Member Agreement and Question/Answer process).

I also noticed that a number of your questions are less than 50 points, which is the recommended value for EASY questions, with 300 points the maximum.  To draw additional attention, you can always increase their values without our assistance.

http://www.experts-exchange.com/jsp/qShow.jsp?ta=wince&qid=20229485
http://www.experts-exchange.com/jsp/qShow.jsp?ta=wince&qid=20123800
http://www.experts-exchange.com/jsp/qShow.jsp?ta=wince&qid=20121605
http://www.experts-exchange.com/jsp/qShow.jsp?ta=wince&qid=20120473
http://www.experts-exchange.com/jsp/qShow.jsp?ta=wince&qid=20110164
http://www.experts-exchange.com/jsp/qShow.jsp?ta=wince&qid=20109749
http://www.experts-exchange.com/jsp/qShow.jsp?ta=wince&qid=20077855
http://www.experts-exchange.com/jsp/qShow.jsp?ta=wince&qid=12019739 November, 2000, zero comments added, 5 points.
http://www.experts-exchange.com/jsp/qShow.jsp?ta=winprog&qid=11169491 November, 2000 ZERO points, comments added
http://www.experts-exchange.com/jsp/qShow.jsp?ta=cplusprog&qid=20135373
http://www.experts-exchange.com/jsp/qShow.jsp?ta=cplusprog&qid=20133207
http://www.experts-exchange.com/jsp/qShow.jsp?ta=cplusprog&qid=20115081
http://www.experts-exchange.com/jsp/qShow.jsp?ta=cplusprog&qid=20103624
http://www.experts-exchange.com/jsp/qShow.jsp?ta=cplusprog&qid=20102120
http://www.experts-exchange.com/jsp/qShow.jsp?ta=cplusprog&qid=20100568
http://www.experts-exchange.com/jsp/qShow.jsp?ta=cplusprog&qid=20088761
http://www.experts-exchange.com/jsp/qShow.jsp?ta=cplusprog&qid=20076668
http://www.experts-exchange.com/jsp/qShow.jsp?ta=cplusprog&qid=11536619 October, 2000 - 15 points
http://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=12014539  November, 2000
http://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=11990378      "
http://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=11863978      "
http://www.experts-exchange.com/jsp/qShow.jsp?ta=mfc&qid=20223052
http://www.experts-exchange.com/jsp/qShow.jsp?ta=mfc&qid=20220148
http://www.experts-exchange.com/jsp/qShow.jsp?ta=mfc&qid=20149200  July, 2001 - 5 points
http://www.experts-exchange.com/jsp/qShow.jsp?ta=mfc&qid=20148518    "      - 10 points
http://www.experts-exchange.com/jsp/qShow.jsp?ta=mfc&qid=20138848
http://www.experts-exchange.com/jsp/qShow.jsp?ta=mfc&qid=20137234
http://www.experts-exchange.com/jsp/qShow.jsp?ta=mfc&qid=20132473
http://www.experts-exchange.com/jsp/qShow.jsp?ta=mfc&qid=20132221
http://www.experts-exchange.com/jsp/qShow.jsp?ta=mfc&qid=20122930  May, 2001 5 points
http://www.experts-exchange.com/jsp/qShow.jsp?ta=mfc&qid=20121466  May, 2001 0 points
http://www.experts-exchange.com/jsp/qShow.jsp?ta=mfc&qid=20094887
http://www.experts-exchange.com/jsp/qShow.jsp?ta=mfc&qid=20094102
http://www.experts-exchange.com/jsp/qShow.jsp?ta=mfc&qid=20067934

I have asked Administration to Email you in these regards.  I also noticed several other things I'd like to bring to you attention.  There are some that you did award with "C" grades, and no comment as to why.  Also, some of your questions have had zero response, and that is likely for two reasons:  Poor closure on open items and poor grades.  I think you'll agree, epecially given the number of questions you've asked here, that experts here take great pride in their work, and deserve "A" grades and responses to all help they've given you.

EXPERTS -->  In the event any of these items remain open at the end of this week, I will finalize them and look to you for guidance on the fair results.  As always, we very much appreciate your willingness to help and the excellent support you all give.

Thanks,

Moondancer
Community Support Moderator @ Experts Exchange
0
 
LVL 11

Expert Comment

by:griessh
ID: 6819129
I suggest to split between Axter and KangaRoo. They gave the two parts of the answer.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 
LVL 5

Expert Comment

by:Netminder
ID: 6822830
Admin notified of user neglect. Force/accepted by

Netminder
Community Support Moderator
Experts Exchange
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now