Avatar billede nerddk Nybegynder
22. september 2004 - 10:48 Der er 3 kommentarer og
1 løsning

constructors, copy-constructors and destructors info

Hi there! Let's start with saying that I do understand Danish, so it's not obligatory to answer in English..!-)

I would like to know a little bit about constructors, destructors and copy constructors. I'm learning from this tutorial (If I remember correctly, it's "Sam's Teach Yourself C++ In 21 Days) - and it uses these constructors, and I understand them (almost) thoroughly. It's just this one thing I'm just eager to know, how you can know when to put braces and when not to:

class SomeClass // No braces
{
public:
  SomeClass();
  SomeClass(const SomeClass &);
  ~SomeClass();
}

class SomeClass // With braces
{
public:
  SomeClass() {};
  SomeClass(const SomeClass &) {};
  ~SomeClass() {};
}

Can someone give me an answer on this and help me to understand this..? Something tells me, it's not that important, but I would really like to know what I'm doing! ;-)
Avatar billede jpk Nybegynder
22. september 2004 - 10:59 #1
Putting the braces there takes care of the implementation. without braces you would have to do this elsewhere e.g. in a .cpp file:

SomeClass::SomeClass() {};
Avatar billede nerddk Nybegynder
22. september 2004 - 11:11 #2
uhmm... but before I even learned to use those braces there, the constructors were just put like this:

S();
~S();
S(const S &);

and then braces were never used. How can that be then?
Avatar billede jpk Nybegynder
22. september 2004 - 14:32 #3
If they are declared like that they MUST be defined elsewhere!
Probably your linker won't complain though, until you actually try to instantiate an object of the class.
Avatar billede nerddk Nybegynder
22. september 2004 - 14:40 #4
hmmm... all right... if you say so =P
I might be wrong, however I'm sure that I could use it without the enclosing braces once in that tutorial. But you're probably right, because I can't remember when I used it. So it could be in the start of the chapter including classes. (Just writing a class, and not actually use it)

Thank you for you response..!-)
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester