CREATING OBJECTS

Discussion in 'C++' started by ben, Apr 28, 2005.

  1. ben

    ben Guest

    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
     
    ben, Apr 28, 2005
    #1
    1. Advertising

  2. ben

    Guest

    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
     
    , Apr 28, 2005
    #2
    1. Advertising

  3. ben

    Rolf Magnus Guest

    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
     
    Rolf Magnus, Apr 28, 2005
    #3
  4. "Rolf Magnus" <> wrote in message news:d4qh0g$i8o$04$-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
     
    Chris \( Val \), Apr 28, 2005
    #4
  5. ben

    ben Guest

    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
     
    ben, Apr 28, 2005
    #5
  6. ben

    Rolf Magnus Guest

    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".
     
    Rolf Magnus, Apr 28, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. bigbinc
    Replies:
    3
    Views:
    407
    Michael Borgwardt
    Nov 18, 2003
  2. Simon Elliott

    Inheritance of objects within objects

    Simon Elliott, Dec 10, 2004, in forum: C++
    Replies:
    2
    Views:
    344
    Simon Elliott
    Dec 10, 2004
  3. =?Utf-8?B?c3RldmVuIHNjYWlmZQ==?=

    form objects into class objects

    =?Utf-8?B?c3RldmVuIHNjYWlmZQ==?=, Jul 5, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    443
    =?Utf-8?B?c3RldmVuIHNjYWlmZQ==?=
    Jul 6, 2006
  4. 7stud
    Replies:
    11
    Views:
    714
    Dennis Lee Bieber
    Mar 20, 2007
  5. News123
    Replies:
    7
    Views:
    390
    Dave Angel
    Jun 29, 2009
Loading...

Share This Page