• Advertisement

Archived

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

this is stupid

This topic is 5063 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
Yes, it''s very stupid to be calling a virtual function in the constructor.

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
Easy.


template<class T>
T* Create()
{
T* obj = new T();
obj->init();
return obj;
}

// usage

B* b = Create<B>();


[edited by - antareus on April 11, 2004 1:42:16 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by antareus
Easy.

// usage
B* b = Create<B>;


*cough* B * b = Create<B>();

Share this post


Link to post
Share on other sites
I too came across this problem. The neatest soloution I could find was to make a template function that creates the object for you and to call the virtual Init() function. It would then return a pointer to the created object. I''m sure there must be a better way, but it did the job for me.

Share this post


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

Hmm.. didn''t my first post explain that enough? Instead of the output of: A::hello() i want it to output B::hello().

quote:

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.

C++ isn''t a language, C++ is three languages(asm, C, C++). So it''s ment to be hacked into making things work the way you want it to. Just look at the window api wrappers out there, is there one that doesn''t have a hack inside it? I don''t think so... Some libraries you could look at is WTL or why not Borland''s VCL, which basically is an entierly new language!

Share this post


Link to post
Share on other sites
quote:
Original post by SiCrane
*cough* B * b = Create<B>();


Oops! Thats what I get for writing code before I take a shower! Cheers.

Share this post


Link to post
Share on other sites
quote:
Write your own language fuck head.


I would if I knew enough about assembly and machine code.

edit: is that kind of language allowed on this board?

[edited by - angry on April 11, 2004 1:48:00 PM]

Share this post


Link to post
Share on other sites
Or just use D. I don''t know if it supports what you''re trying to do (somehow I doubt it). The real question is why do you need a virtual Init function when you can put the contents of the derived Init into a constructor?

Share this post


Link to post
Share on other sites
All D would do is make it look prettier

The true general first seeks victory, then seeks battle
- Sun Tzu

Share this post


Link to post
Share on other sites
quote:
Original post by angry C++ isn''t a language, C++ is three languages(asm, C, C++).

This belief is why you aren''t using c++ well. Clear your mind of this, and everything else will be easier.

Share this post


Link to post
Share on other sites
It makes perfectly good sense for the object to be an A before and during the construction of its A-ness.

Share this post


Link to post
Share on other sites
Yeah. Gee. C++ is hard. Ya''ll should just give up when things are hard.

On a serious note, I''ll agree that there are some brilliantly stupid things in C++, but this isn''t one of them.

Share this post


Link to post
Share on other sites

  • Advertisement