Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

angry

this is stupid

This topic is 5333 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

class A
{
    protected:
          virtual void hello()
          {
              std::cout << "A::hello()" << std::endl;
          }

    public:
          A(A* parent)
          {
              parent->hello();
          }
};

class B: public A
{
     protected:
           virtual void hello()
           {
               std::cout << "B::hello()" << std::endl;
           }

     public:
          B(): A(this)
          {
          }
};
Output: A::hello() [edited by - angry on April 11, 2004 2:37:58 AM]

Share this post


Link to post
Share on other sites
Advertisement
Calling a virtual function on a pointer to an incompletely constructed object is non-kosher. In this case when the B object invokes the A constructor, it isn''t a B object yet, its still only an (incomplete) A object.

Share this post


Link to post
Share on other sites
I've had it with this language! Now it's final, I'll quit programming C++ and begin programming in C# instead!

[edited by - angry on April 11, 2004 3:02:00 AM]

Share this post


Link to post
Share on other sites
And C# will fix your problem how? It won''t let you do it either. Specifically, it won''t let you pass a this pointer to the base constructor.

Share this post


Link to post
Share on other sites
Java''s also the same way; it won''t let you use the this pointer until after the superclass constructor has run.


"Sneftel is correct, if rather vulgar." --Flarelocke

Share this post


Link to post
Share on other sites
The other languages might not solve this problem, however there are a couple of hundered more annoying things in C++ that I don''t like. However most is that there isn''t a global coding design guide, which makes the code so damn ugly if you use more than zero libraries...

Anyways, what I wanted to do was to create a virtual init() method that was called after all the constructors. So this init method would work like an virtual constructor in a way.

Does anyone have an idea of how I could create this virtual init method of mine?
I want it to do the same as executing the following lines:

MyClass* cls = new MyClass;
cls->init();

delete cls;

I tried overloading the operator new, however that operator doesn''t call the constructor until after it has returned. So this was not possible:

void* operator new(unsigned int size)
{
char* mem = new char[size];
reinterpret_cast(mem)->init();
return mem;
}

And besides the virtual table is not created at this point, which makes sense, so this would cause an invalid pointer access exception or something like that, i.e the program will crash.

I''ve also tried this:

void* operator new(unsigned int)
{
MyClass* newInstance = ::new MyClass;
newInstance->init();
return newInstance;
}

However as the constructor isn''t called until after the new operator has returned this will make the constructor called twice, which isn''t good at all.

So this seems like an unsolvable problem unless you can disable the constructor call after new somehow. Or maybe someone knows of an solution in assembler?

I want virtual constructors now!

Share this post


Link to post
Share on other sites
Seems to me that you need to work on using the language correctly, instead of bitching about how something doesn''t work how YOU want it do.

Share this post


Link to post
Share on other sites
if you explain exactly what you are trying to accomplish, someone might be able to help you with a (valid) C++ way of doing it...

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!