segmentation fault issue in derived class assigment [modified]
-
hi this code causes segmentation fault can u pleases y.
class ab
{
public:
virtual void abb()=0;
};
class bc : public ab
{
public:
void abb()
{
cout<<"here";
}
};
void get(ab* c)
{
bc *cc = new bc();
c = cc;//i know this is culprit
}
int main()
{
ab *b = NULL;
get(b);
b->abb();
}but how can i solve this so that i need to fill derived class object (reference or pointer) to base class in get function only.. [my request]:confused: if u feel iam very poor in basics for give me plz by only this i can learn more and attain like u [/my request] :((
modified on Tuesday, May 19, 2009 2:03 AM
-
hi this code causes segmentation fault can u pleases y.
class ab
{
public:
virtual void abb()=0;
};
class bc : public ab
{
public:
void abb()
{
cout<<"here";
}
};
void get(ab* c)
{
bc *cc = new bc();
c = cc;//i know this is culprit
}
int main()
{
ab *b = NULL;
get(b);
b->abb();
}but how can i solve this so that i need to fill derived class object (reference or pointer) to base class in get function only.. [my request]:confused: if u feel iam very poor in basics for give me plz by only this i can learn more and attain like u [/my request] :((
modified on Tuesday, May 19, 2009 2:03 AM
First of all, you have forgotten to derive bc from ab here.
hawk23reddy wrote:
class bc
Secondly, change the signature of get from
hawk23reddy wrote:
void get(ab* c)
to
hawk23reddy wrote:
void get(ab** c)
and while calling get, pass address of b as argument.
-
hi this code causes segmentation fault can u pleases y.
class ab
{
public:
virtual void abb()=0;
};
class bc : public ab
{
public:
void abb()
{
cout<<"here";
}
};
void get(ab* c)
{
bc *cc = new bc();
c = cc;//i know this is culprit
}
int main()
{
ab *b = NULL;
get(b);
b->abb();
}but how can i solve this so that i need to fill derived class object (reference or pointer) to base class in get function only.. [my request]:confused: if u feel iam very poor in basics for give me plz by only this i can learn more and attain like u [/my request] :((
modified on Tuesday, May 19, 2009 2:03 AM
-
Try this:
...
void get(ab* **&**c)
...Research passing arguments to functions and especially passing arguments by reference.
thank u. i got it..