0

I have a class for loading and render objects in OpenGL. For loading objects I have a function called LoadFile(std::string FilePath) which work just fine but now I also want to load a file from the constructor so I tried doing this:

CObject(std::string FilePath) { CObject(); // set all values to 0 LoadFile(FilePath); } 

But doing this crashes my app whenever I try to render and I really have no idea why :s.

2
  • Error message? Did you try a debugger? What's in CObject()? And please work on your acception rate. Commented Apr 26, 2012 at 18:10
  • Better create another member function which does those assignments and call it in constructor. Commented Apr 26, 2012 at 18:11

3 Answers 3

2

Well, CObject(); doesn't set all values to 0, but creates a temporary object. It should be:

CObject(std::string FilePath) { //manually set fields to 0 LoadFile(FilePath); } 

If your default constructor also calls itself again, it's most probably a stack overflow error.

Sign up to request clarification or add additional context in comments.

3 Comments

@ildjarn does pointing out something wrong in a different answer not count?
Apparently to some people... ;-/
I did that but the I get the same problem.
2

Calling a constructor of the same class from within a constructor won't work as Java (that is delegate some of the job to a more generic constructor). That syntax there just means that you're creating a new temporary object with the default constructor.

The only close thing is C++11's delegated constructor:

CObject(std::string FilePath): CObject() { LoadFile(FilePath); } 

24 Comments

Sure you CAN call a constructor from within a constructor.
@Luchian Grigore In C++03 you cannot call a constructor from another constructor of the same class for the same object. Also you don't have to explicitly specify every single detail that can be understood from context.
@n.m. What if the op is a beginner? He sees someone with 700+ rep on SO that says "You cannot call a constructor from within a constructor" and takes this as a rule, because someone inexperienced can't see the context you see. You have to specify the details, and this isn't just a detail. It's wrong!. I can't believe I'm arguing about this.
@Traxmate: A few hundred lines long uncompilable fragment of code is not anyone's idea to spend an hour. If you can't narrow the problem to a small compilable self-contained example, then the question is not for this site.
@n.m. Ok I see. Lorenzo edited the question and, IMO, that's enough. And I really hope it's just stubbornness on your behalf that makes you go on with this. You do realize that you're saying "It's ok to give out false information, people can read between the lines"? I'm not going to waste any more time with your nonsense.
|
1

When you call the CObject() constructor to set all values to 0. You are actually creating a temporary CObject instead of setting all of the current objects values to 0.

What you need to do is either create a private initialization method that both the default and other constructors can call before loading a file or initialize the objects members in the CObject(std::string FilePath) constructor.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.