Jump to content
  • Advertisement

Archived

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

Mayrel

C++ Constructors

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

Hullo, I''ve had a bit of a problem with my constructors. The following illustrates the issue:
  
  class Foo
  {
    Foo (int x);
    Foo (int x, char *y);
    Foo (int x, double y);
    Foo (int x, Wombat *y);
  }

  Foo::Foo (int x, char *y)
    : Foo(x)
  {
    /* char* specifific stuff */
  }
  
My compiler (VC++) complains that ''Foo(int)'' isn''t a member of ''Foo'', which seems blatently wrong to me. Is my compiler right? If it is, how am I supposed to call one constructor from another. All your bases belong to us (I know. It''s irony.)

Share this post


Link to post
Share on other sites
Advertisement
After the ":" with a constructor implementation,
you initialize VARIABLES with var(value), it''s
not allowed to do function calls *there*.

Just call your overloaded func in the body,
like this:

Foo::Foo(....)
{
Foo(x);
}

Share this post


Link to post
Share on other sites
Shouldn''t that create an temporary instance of Foo() and then throw it away? Or does C++ know to treat calls to a constructor specially if I''m in a constructor at the time?

All your bases belong to us (I know. It''s irony.)

Share this post


Link to post
Share on other sites
Well,I''m not sure if c++ "knows" it,
but I think I did something like this, and
can''t remember that the program did something
I didn''t tell it to do, so I suppose it works.

Well, what are u gonna do?
Do you init some vars, or allocating some mem in
the constructor? Maybe you won''t need to call
that function, but,
however, I think it works.
Just try.

What I wanted to tell you, is only that the
syntax of the constructor implementation is
like I said above, and the "()" after the
name doesn''t have anything to do with a function,
although it may look like that.
It sets a classe''s variable to (value),
nothing more.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
maybe you should make an initialize function. You could then use it for other things.

Share this post


Link to post
Share on other sites
I don''t think that anyone else pointed this out but you don''t have a public, private, or protected section of your class, thats ok, but constructors have to be public and class members are private by default.

Jason Mickela
ICQ : 873518
E-Mail: jmickela@sbcglobal.net
------------------------------
"Evil attacks from all sides
but the greatest evil attacks
from within." Me
------------------------------

Share this post


Link to post
Share on other sites
quote:
Original post by griffenjam
you don''t have a public, private, or protected section of your class, thats ok, but constructors have to be public


Actually, I dont think that constructors have to be public. I''ve seen many instances where there are private constructors. Why, I''m not sure.

Share this post


Link to post
Share on other sites
Mayrel, what your doing is illegal. After the parameter list in a constructor the ":" is fallowed by variables of the class and there initial values. THE EXCEPTION IS BASE CLASSES. You are free to put a call to your base class constructor in the intialization list. However this doesn''t seem to be what you are doing.

The AP had probably the best idea.
To get your desired result, have a funtion called Init(x) that will do everything with the x variable. So the function Foo(int x) simply calls Init(x) and the others, like Foo(int x, char * y) call Init(x) then do their own thing.

Calling a plain constructor is illigal.
This code will not compile.
Foo::Foo(int x, char * y)
{
Foo(x);
}

RUn it in your compiler.

One last thing, there are several good reasons for having private constructors. One is when you want only a handful of classes to be able to make one of your objects, but want all to see it. By declaring the constructors private and haveing classes that can build one as Friends, you can keep others from doing something you don''t want. (if it''s jsut one class that can build, its better just to make it a private declaration in the class).

Anyway thats my little piece.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by DanG
Mayrel, what your doing is illegal.


Yeah. Im gonna report him to the FBI.

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!