Sign in to follow this  

Copy Constructor(nevermind)

This topic is 4867 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

I'm having a bit of difficulty with my copy constructor. It seems that the compiler doesn't want to call it, favoring a method that requires an intermidairy step.
[source=cpp]
class B
{
public:
operator int*();
};
class A : public B
{
public:
A(const B& rs);
A(int* input);
};
void main()
{
B Stuff;
A Blah(Stuff);
}



For some reason it decides to call operator int*, then construct with that pointer (NO! BAD!), instead of just constructing from B itself. How can the compiler chose to go through 2 steps when a single step method is available? I'm using Microsoft Visual C++ 2005 Express Edition. I don't think it's a beta bug, because I recall having similar difficulties with VC6 with it chosing to first cast to char*, then cast the char* to bool, instead of using my operator bool. I instead used operator !, and then did !! for true and just ! for false. I got it to work in 2k5 by putting operator bool above the char* cast, but as you can see in the above code, tried that, didn't work. Edit: Found my problem, check 3rd post. [Edited by - Erzengeldeslichtes on August 20, 2004 5:06:44 PM]

Share this post


Link to post
Share on other sites
No, I just tried it and it works as well. Grrr. The full source is thusly: (beware, quite long)
**snip**
then in an object I have a PartialPointer<Base, false> m_This, which I plug into a function that expects a Pointer<Base, false>. I have to cast it either by doing Pointer<Base, false>(m_This), or (Pointer<Base, false>)m_This, either way will do the same thing. If you skim through, you will see a Pointer(const PartialPointer& rs), but it insists on casting to a Base*, then back to a Pointer. (which causes an access violation in the near future) So it choses to go with the 2 step, crash causing method, rather than the (higher in the class,) 1 step, should-work-fine method.

For a test, take the above and stuff it into Pointer.h, and put the below into main.cpp:
**snip**
Put a breakpoint on reversion, and you'll see it go to int* then back to Pointer.

Edit: Found my problem. I should have used Pointer(const PartialPointer<T, UseExistsFunction>& rs) instead of one without template parameters.

[Edited by - Erzengeldeslichtes on August 20, 2004 5:25:52 PM]

Share this post


Link to post
Share on other sites

This topic is 4867 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.

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