473,387 Members | 1,517 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

CREATING OBJECTS

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

Jul 23 '05 #1
5 1462
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

Jul 23 '05 #2
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


Jul 23 '05 #3

"Rolf Magnus" <ra******@t-online.de> wrote in message news:d4*************@news.t-online.com...
| 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
Jul 23 '05 #4
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

Jul 23 '05 #5
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".
Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: vande | last post by:
Hi, I am trying to create com objects in asp.net. The objects are getting created but the context in the object created is null. If I create the same object in asp the object is ok and the...
8
by: Nanda | last post by:
hi, I am trying to generate parameters for the updatecommand at runtime. this.oleDbDeleteCommand1.CommandText=cmdtext; this.oleDbDeleteCommand1.Connection =this.oleDbConnection1;...
2
by: JJ L. | last post by:
Hello. I have a project that consists of nine different objects, each serving their own purpose. In the past I have just created a form for each one, and then whenever you call, say,...
5
by: Chris | last post by:
Hi, to create an array of 2 objects (e.g. of type '__gc class Airplane') I need to do : Airplane * arrAirplanes __gc = new Airplane* __gc; arrAirplanes = new Airplane("N12344"); arrAirplanes...
3
by: Ken Varn | last post by:
I am just starting the process of creating ASP.NET server controls. I have created controls for .NET applications, but have just started with ASP.NET. I am a little confused about some areas that...
5
by: | last post by:
Trying to learn about manipulating collections of objects, and populating these objects dynamically from datasources. Could someone post a code sample that shows the following: Instantiating a...
1
by: chris | last post by:
I know I've asked this before, but I didn't really get an answer and I bet it's because I didn't explain myself very well. Here goes again. I have this code: Dim arrData(intNoOfRows,...
5
by: fireball | last post by:
please help newbie I need to create a lot of objects the same type (let's say: schemas) I wish to use paramerized block in loop to do so. - how to put names of my objects to such control-flow? ...
31
by: JoeC | last post by:
I have read books and have ideas on how to create objects. I often create my own projects and programs. They end up getting pretty complex and long. I often use objects in my programs they are...
13
by: jkimbler | last post by:
As part of our QA of hardware and firmware for the company I work for, we need to automate some testing of devices and firmware. Since not everybody here knows C#, I'm looking to create a new...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.