Advertisement

Newbie C++ Question...

Started by May 19, 2000 10:10 PM
4 comments, last by ziplux 24 years, 7 months ago
Hi. I''m a bit of a C++ newbie, so please bear with me. Lets say I have a class that requires initilization (spelled wrong, I think I''m a programmer, not an English teacher ). Anyway, show the init code be put in a put in the constructor or a public function called Init(...)? Is there a more "correct" way, or is it preference? Thanks in advance. Visit our web site: Asylum Entertainment
My Geekcode: "GCS d s: a14 C++$ P+(++) L+ E-- W+++$ K- w++(+++) O---- M-- Y-- PGP- t XR- tv+ b++ DI+(+++) D- G e* h!"Decode my geekcode!Geekcode.com
Visit our web site:Asylum Entertainment
teehee bear with him
There's always something smaller and something bigger. Don't sweat the small stuff and don't piss off the big stuff :)
Advertisement
If the initialization is not needed anymore after the object is created, you can just put code in the constructor. If you want to call the initialization function more than once, make a seperate function for it.

---
"I hate television. I hate it as much as peanuts. But I can''t stop eating peanuts." - Orson Welles
SoGreen: What is SoFunny?

Densun: Thanks for your help. I''ll probably be using the constructor.

Visit our web site:
Asylum Entertainment
My Geekcode: "GCS d s: a14 C++$ P+(++) L+ E-- W+++$ K- w++(+++) O---- M-- Y-- PGP- t XR- tv+ b++ DI+(+++) D- G e* h!"Decode my geekcode!Geekcode.com
Visit our web site:Asylum Entertainment
Btw, you should also look into initializer lists for constructors as they are the preferred way of setting up data. For instance, in the following 2 examples, they are more or less equivalant but the 2nd is preferred:

MyClass::MyClass(int somevalue){    memberA = somevalue;    memberB = SOME_CONST;}MyClass::MyClass(int somevalue) :memberA(somevalue), memberB(SOME_CONST){} 


Although it makes little difference when you''re just passing and initialising basic data members, when you start making classes that contain other classes, it is faster to initialise them in the list as that way you only need to make 1 constructor call, rather than a basic constructor call followed by another to set up the value.
Kylotan, thanks for pointing that out again, I keep forgetting that it is possible to use initialiser lists.
I have a problem in my current code that my constructors are terribly slow, and that''s JUST the solution I SHOULD have been looking for


#pragma DWIM // Do What I Mean!
~ Mad Keith ~
It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.

This topic is closed to new replies.

Advertisement