Inheritance issue [modified]
-
hello :) I've found my silly mistake please help me find error in my code :
// *************
class Base
{
public :virtual ~Base() {}; // Retrieve value of X virtual void getX() const { cout<// should be &pError here { pError->getX(); **// and .getX() here** } return 0;
}
sorry for stupid question I am pretty tired, need a good sleep :) -- modified at 9:42 Monday 29th May, 2006
-
hello :) I've found my silly mistake please help me find error in my code :
// *************
class Base
{
public :virtual ~Base() {}; // Retrieve value of X virtual void getX() const { cout<// should be &pError here { pError->getX(); **// and .getX() here** } return 0;
}
sorry for stupid question I am pretty tired, need a good sleep :) -- modified at 9:42 Monday 29th May, 2006
big_denny_200 wrote:
virtual void getX() const { cout<;
don't close your functions definitions with semicolons (';'). at last, just curious... don't you understand what the compiler says when complaining ?! ;P i have no compiler to test here, so, sorry.
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ] -- modified at 9:45 Monday 29th May, 2006
-
big_denny_200 wrote:
virtual void getX() const { cout<;
don't close your functions definitions with semicolons (';'). at last, just curious... don't you understand what the compiler says when complaining ?! ;P i have no compiler to test here, so, sorry.
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ] -- modified at 9:45 Monday 29th May, 2006
no :( why not use semicolons ? thanks -- modified at 9:34 Monday 29th May, 2006
-
no :( why not use semicolons ? thanks -- modified at 9:34 Monday 29th May, 2006
big_denny_200 wrote:
why not use semicolons ?
and you didn't answer my questions more. * What are the compiler errors ?
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ] -- modified at 9:45 Monday 29th May, 2006
-
hello :) I've found my silly mistake please help me find error in my code :
// *************
class Base
{
public :virtual ~Base() {}; // Retrieve value of X virtual void getX() const { cout<// should be &pError here { pError->getX(); **// and .getX() here** } return 0;
}
sorry for stupid question I am pretty tired, need a good sleep :) -- modified at 9:42 Monday 29th May, 2006
it is not the problem with a ";" problem is with ur try catch block;
big_denny_200 wrote:
catch (Base *pError)
u r catching a pointer to the throwing object but inside the try block u r doing
big_denny_200 wrote:
throw Derived1(); // when I add this line I get assert
so inside the throw block do as follows Derived1 d; // when I add this line I get assert throw &d; or throw &Derived1(); // when I add this line I get assert u can also resolve if u change the catch block to pass by value instead pass by reference SaRath
"D on't blindly follow rules you read somewhere without verifying that it makes sense for your situation!" -
big_denny_200 wrote:
why not use semicolons ?
and you didn't answer my questions more. * What are the compiler errors ?
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ] -- modified at 9:45 Monday 29th May, 2006
toxcct wrote:
because the C++ doesn't want them here
In fact, this won't change anything. You can let them there. They will simply be ignored.
Cédric Moonen Software developer
Charting control -
big_denny_200 wrote:
why not use semicolons ?
and you didn't answer my questions more. * What are the compiler errors ?
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ] -- modified at 9:45 Monday 29th May, 2006
-
toxcct wrote:
because the C++ doesn't want them here
In fact, this won't change anything. You can let them there. They will simply be ignored.
Cédric Moonen Software developer
Charting control -
it is not the problem with a ";" problem is with ur try catch block;
big_denny_200 wrote:
catch (Base *pError)
u r catching a pointer to the throwing object but inside the try block u r doing
big_denny_200 wrote:
throw Derived1(); // when I add this line I get assert
so inside the throw block do as follows Derived1 d; // when I add this line I get assert throw &d; or throw &Derived1(); // when I add this line I get assert u can also resolve if u change the catch block to pass by value instead pass by reference SaRath
"D on't blindly follow rules you read somewhere without verifying that it makes sense for your situation!"SaRath C wrote:
Derived1 d; // when I add this line I get assert throw &d;
Never do this as it’s seriously flawed. The object
d
is on the stack but the stack is unwound when an exception is caught. The upshot is that when you catch an exception with such code the exception handler gets a pointer to an object which no longer exists. It's the same situation as returning a reference or pointer to a local variable from a function. Steve -
hello :) I've found my silly mistake please help me find error in my code :
// *************
class Base
{
public :virtual ~Base() {}; // Retrieve value of X virtual void getX() const { cout<// should be &pError here { pError->getX(); **// and .getX() here** } return 0;
}
sorry for stupid question I am pretty tired, need a good sleep :) -- modified at 9:42 Monday 29th May, 2006
You're throwing a
Derived1
but catching aBase*
. If you want to catch aBase*
you need code like this:try { throw new Derived1(); } catch (Base *pError) { pError->getX(); delete pError; }
A better was is not to throw a pointer. Use code like this:try { throw Derived1(); } catch (const Base &e) { e.getX(); }
Steve