When using class inheritance, class doing inheriting calls constructor as program end

Started by
4 comments, last by johnnyBravo 19 years, 6 months ago
Hi, I'm using class inheritance for my app eg edit:I forgot i had the Rock class actually declared in another class.

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;
}


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]
Advertisement
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
-----------------------------"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning..." -- Rich Cook"...nobody ever accused English pronounciation and spelling of being logical." -- Bjarne Stroustrup"...the war on terror is going badly because, if you where to compare it to WWII, it's like America being attacked by Japan, and responding by invading Brazil." -- Michalson
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 =).
Things change.
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!
the following code compiles and works fine :
#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 ?
- stormrunner
Well the app class inherits from a CApplication class, which conttains the init() etc, which are run in the CApplication constructor...


eg.
class CApplication {
protecteted:
void run(){}
void init(){}

public:
CApplication () {
init();
run();
}

};

This topic is closed to new replies.

Advertisement