This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: create an object with variable CLASS
Hi,
On Wed, Oct 28, 2009 at 06:07:32AM +0330, Mohsen Pahlevanzadeh wrote:
> You caused i open OOP in C++ book & start reading.My skill is python
> programming & C.
> But i have a question about previos source:
> When i compile my source, i get seg fau, When i run it with gdb, i rcv
> following message:
> _IO_vfscanf () from /lib/i686/cmov/libc.so.6
> Then i changed create_object to following code :
> module_management* create_object(char * class_name){
> if (c_find(class_name) == NULL){
> if (strcmp (class_name,"mikrotik") == 0){
> mikrotik *iu ;
> return iu;//new mikrotik;
> }//end of strcmp
> }//end of c_find if block
> exit(1);
> }//end of func
> /////////////////////////////////////
>
> When i compile it, i use following error with GDB:
> 250 create_object("mikrotik")->printg();
> Current language: auto
>
> /////////////////////////////////////////////////////
> I guess it return same Base object, because i tested with following
> function:
> cout <<typeid(create_object("mikrotik")).name() << endl;
> and i see it printed type of Base classs of mikrotik.
>
> Do you have any idea?
We can't tell you all errors - therefore it would be necessary that you
send the complete source code. The best is always a file as short as
possible which contains all parts of the source code to produce the error.
However, there is at least one error in your code:
mikrotik *iu ;
return iu;//new mikrotik;
"iu" is a pointer to an object of class mikrotik. Then you return "iu".
However, you DON'T CREATE the object by using "new" - in this situation,
it is completely undefined what happens when you access "iu".
You have to write either:
mikrotik *iu ;
iu = new mikrotik;
return iu;
or
return new mikrotik;
or
mikrotik *iu = new mikrotik;
return iu;
or something like that
Axel.
>
>
>
>
> On Wed, 2009-10-28 at 03:11 +0330, Mohsen Pahlevanzadeh wrote:
> > Oh, shit, Thank you Andrew & Axel.
> > On Tue, 2009-10-27 at 11:34 +0000, Andrew Haley wrote:
> > > Mohsen Pahlevanzadeh wrote:
> > > > My main file:
> > > > int main(){
> > > > module_management *x;
> > > > x.printg();
> > > > }
> > >
> > > Well, that's not going to work, is it? x is a pointer, not
> > > an object. You need
> > >
> > > x = create_object("mikrotik");
> > > x->printg();
> > >
> > > Andrew.
> >
> >
>