Sign in to follow this  
johnnyBravo

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

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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 =).

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
the following code compiles and works fine :

#include <iostream>
#include <cstdlib>
#include <windows.h> // couldn't remember where Beep was defined
class 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 ?

Share this post


Link to post
Share on other sites
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();
}

};

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this