• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 274
  • Last Modified:

tricky or not

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
Shay050799
Asked:
Shay050799
1 Solution
 
AxterCommented:
No.

The veriable you are passing is not a pointer.

What exactly do you want to do, what is your goal?
0
 
proskigCommented:
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
 
KangaRooCommented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
NickAtBOMCommented:
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
 
NickAtBOMCommented:
KangaRoo - you got that in a whole min earlier...
0
 
elcapitanCommented:
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
 
AxterCommented:
elcapitan,
The questioner asked for a C method.
0
 
AxterCommented:
elcapitan,
Disregard my previous comment.  I got my questions confused.
Sorry
0
 
MoondancerCommented:
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
 
griesshCommented:
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
 
NetminderCommented:
Admin notified of user neglect. Force/accepted by

Netminder
Community Support Moderator
Experts Exchange
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now