CREATING OBJECTS

B

ben

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
 
G

gareth_stockwell

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
 
R

Rolf Magnus

ben said:
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.
 
C

Chris \( Val \)

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

ben

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
 
R

Rolf Magnus

ben said:
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".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top