class Object {
public:
Object (){}
};
class Rock: public Object {
public:
Rock() {Beep(100,100); }
};
class App {
public:
Rock rock;
};
int main() {
App app;
int a; cin <<a;
return 0;
}
When using class inheritance, class doing inheriting calls constructor as program end
Hi, I'm using class inheritance for my app
eg
edit:I forgot i had the Rock class actually declared in another class.
this is only an example of my classes
When 'Rock rock' is initialised nothing happens, but when the app quits, it Beeps.
Anyone know why the Rock's constructor is being run at the end, which in effect it is behaving like a destructor.
Thanks
[Edited by - johnnyBravo on October 15, 2004 12:00:05 AM]
Hi!
I'm pretty sure the constructor get's called at the right time. I don't know what Beep() does, and neither do I know why it would beep not when it's supposed to. But first thing I'd try is to replace Beep() by some other feedback method, some console output maybe (and make sure you flush the output stream, using std::endl for example).
Hope that helps!
Cheers,
Drag0n
I'm pretty sure the constructor get's called at the right time. I don't know what Beep() does, and neither do I know why it would beep not when it's supposed to. But first thing I'd try is to replace Beep() by some other feedback method, some console output maybe (and make sure you flush the output stream, using std::endl for example).
Hope that helps!
Cheers,
Drag0n
I think that's actually the constructor being called, but with a natural system delay. Try manually creating the object, waiting for a second, and manually calling the system destructor (there are a lot of ways to "wait a second"...). If you need any more help/that doesn't work, I'll grab a code snippet for you and help find a solution if I can. Never used Beep() before =).
Beep is just some window function, but it does work in console fine.
My app is actually in a win32 app, but its a bit hard to put all that code in here, I have copied from how my classes are setout exactly to the example that is shown.
I have no idea why it is doing this, and it is quite annoying!
My app is actually in a win32 app, but its a bit hard to put all that code in here, I have copied from how my classes are setout exactly to the example that is shown.
I have no idea why it is doing this, and it is quite annoying!
the following code compiles and works fine :
so there must be a problem occuring somewhere else in your code. what else does your app class do ?
#include <iostream>#include <cstdlib>#include <windows.h> // couldn't remember where Beep was definedclass Object { public: Object() {} };class Rock : public Object { public: Rock(){ Beep(100, 100); std::cout << "yo, i've been called" << std::endl; } };class App { public: Rock rock; };int main(int argc,char* argv[]){ App app; std::cout << "This is called directly after App declaration" << std::endl; //Rock rock; int a;// in your code you have cin << a; - should be cin >> a; std::cin >> a; return 0;}
so there must be a problem occuring somewhere else in your code. what else does your app class do ?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement