Solved

tricky or not

Posted on 2001-06-14
11
263 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
mixing C++ & C# in Vis Studio 2013 7 280
sorting efficency of sorting algorithm 30 151
C++ help/ Toy problem 19 71
using interface in TLB 3 30
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

739 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