Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > CREATING OBJECTS

Reply
Thread Tools

CREATING OBJECTS

 
 
ben
Guest
Posts: n/a
 
      04-28-2005
Hi guyz,
Is it possible to create objects of a class whose
definition as well as declaration is not available in advance?

Consider the following program:

#include<iostream>
#include<fstream>


#include "abc.cpp" /*this contains class def. which is not available in
advance, but will have some definition later on .This will be
provided by the user and will be copied there(by opening
abc.cpp,copying the class defn. and closing it)

Now is it possible to create an object of this class in the main
program??*/


using namespace std;

int main()
{

//some code which will prompt user to enter class defn
//the defn. will be copied to abc.cpp
//say the user provided some class defn. like class student{ ...};

// How to create objects of that particular class i.e. student s;


return 0;
}


Note:It is assumed that user has provide a perfect error free class
definition.

bye,
ben

 
Reply With Quote
 
 
 
 
gareth_stockwell@hotmail.com
Guest
Posts: n/a
 
      04-28-2005
Ben,

The definition of a class has to be available to the compiler at the
point when you try to instantiate an object of that class. So the
compiler has to know what a Student is, before it sees the line

Student s;

What you can do is to have a family of classes which derive from a
common base class; read up on polymorphism to find out how this works.
Briefly:

struct Base { /* ... */ };
struct Derived1 : public Base { /* ... */ };
struct Derived2 : public Base { /* ... */ };

In your main function:

Base *b1 = new Derived1;
Base *b2 = new Derived2;

Here, though, the compiler must have seen the definition of Derived1 /
Derived2. However, if the code which does the 'new' operations is
encapsulated in a loadable module, then the actual type of the derived
class could be provided at runtime. This code would have had to have
been compiled into a library, however, so it isn't the same as your
example, in which you wanted source code dynamically `inserted' into a
program. AFAIK, that you can't do.

Gareth

 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      04-28-2005
ben wrote:

> Hi guyz,
> Is it possible to create objects of a class whose
> definition as well as declaration is not available in advance?


No.

> Consider the following program:
>
> #include<iostream>
> #include<fstream>
>
>
> #include "abc.cpp" /*this contains class def. which is not available in
> advance, but will have some definition later on .


Not sure what you mean here. If you #include a class definition, it is
available.

> This will be provided by the user and will be copied there(by opening
> abc.cpp,copying the class defn. and closing it)


"the user"?

> Now is it possible to create an object of this class in the main
> program??*/
>
>
> using namespace std;
>
> int main()
> {
>
> //some code which will prompt user to enter class defn
> //the defn. will be copied to abc.cpp
> //say the user provided some class defn. like class student{ ...};


That sounds to me as if you want to compile a class definition into
executable code at program runtime. This is not possible with C++. If you
want to do something like that, you need to use an interpreted language
like e.g. python.

> // How to create objects of that particular class i.e. student s;
>
>
> return 0;
> }
>
>
> Note:It is assumed that user has provide a perfect error free class
> definition.
>
> bye,
> ben


 
Reply With Quote
 
Chris \( Val \)
Guest
Posts: n/a
 
      04-28-2005

"Rolf Magnus" <> wrote in message news:d4qh0g$i8o$04$...
| ben wrote:
|
| > Hi guyz,
| > Is it possible to create objects of a class whose
| > definition as well as declaration is not available in advance?
|
| No.

[snip]

| > int main()
| > {
| >
| > //some code which will prompt user to enter class defn
| > //the defn. will be copied to abc.cpp
| > //say the user provided some class defn. like class student{ ...};
|
| That sounds to me as if you want to compile a class definition into
| executable code at program runtime. This is not possible with C++. If you
| want to do something like that, you need to use an interpreted language
| like e.g. python.

[snip]

It sounds that way to me too

I would just like to make the OP aware of
a nice interpreter, and not only for C++:
http://www.softintegration.com/

Cheers,
Chris Val


 
Reply With Quote
 
ben
Guest
Posts: n/a
 
      04-28-2005
Hi rolf,
U got exactly what i was trying to tell.Yes i want to compile
a class definition into executable code at program runtime.

And i already got one suggestion i.e. making use of polymorphism.
Thanks anyway.
bye,
ben

 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      04-28-2005
ben wrote:

> Hi rolf,
> U got exactly what i was trying to tell.Yes i want to compile
> a class definition into executable code at program runtime.
>
> And i already got one suggestion i.e. making use of polymorphism.
> Thanks anyway.


Well, that won't make it possible to compile code at runtime, but together
with system specific extensions, it make it possible to load compiled code
from a library at runtime. This is one way of providing "plug-ins" or
"dynamic modules".


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
creating garbage collectable objects (caching objects) News123 Python 7 06-29-2009 04:12 PM
class objects, method objects, function objects 7stud Python 11 03-20-2007 06:05 PM
Unable to serialize the session state. Please note that non-serializable objects or MarshalByRef objects are not permitted when session state mode is 'StateServer' or 'SQLServer'. Mike Larkin ASP .Net 1 05-23-2005 12:33 PM
Inheritance of objects within objects Simon Elliott C++ 2 12-10-2004 10:59 AM
objects of objects, vectors and sessions bigbinc Java 3 11-18-2003 09:26 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57