0

i have this code snippet

class Osoba{
         Osoba(char* imie,int wiek){                         
                     this->imie=new char[strlen(imie)+1];
                     strcpy(this->imie,imie);
                     this->wiek=wiek;
                     cout<<"Utworzono Osobe "<<this->imie<<endl;
         }
         Osoba(Osoba& x){
                 Osoba(x.imie,x.wiek);
         }
[...]

and when i call the copy constructor it doesnt work (creates and destroyes object).

Edit: if i use

         Osoba(Osoba& x): Osoba(x.imie,x.wiek){

i get type 'class Osoba' is not a direct base of 'Osoba'

how is this done ?

n00b
  • 5,642
  • 2
  • 30
  • 48
  • 1
    Use `std::string` instead of `char *` to represent strings in C++. It makes things much simpler. Also, your title suggests something about initialization lists, but you are not using any on the code you show. – Björn Pollex May 17 '11 at 12:32
  • [http://stackoverflow.com/questions/1598967/benefits-of-initialization-lists](http://stackoverflow.com/questions/1598967/benefits-of-initialization-lists) – Bo Persson May 17 '11 at 12:34
  • @c0wb0y i know its just for educational purposes -> why i use char* (dynamic memory alloc etc) – n00b May 17 '11 at 16:36

4 Answers4

10

You can't call constructors like that. Well you can, but what happens is a nameless temporary object gets created. Write your copy constructor without reference to the other constructor. Also, if you use std::string instead of char *, you won't need a copy constructor. If you persist in using char *, you will also need a destructor and an assignment operator.

  • are u 100% sure u cant re-use other constructors ?, and yes i have a destructor i know about string :) – n00b May 17 '11 at 12:31
  • @noob You can't re-use other constructors, but you can sometimes factor out the code into a non-constructor private member function. –  May 17 '11 at 12:35
  • 1
    @noob: and don't forget to define the copy constructor properly: Osoba(**const** Osoba& x) – Kirill Kovalenko May 17 '11 at 12:35
3

You have to initialize the members of Osoba, the same way as you are doing in the other constructor.

You can only reuse constructors (with other syntax) in C++2011.

Didier Trosset
  • 36,376
  • 13
  • 83
  • 122
1

You cannot call a constructor except for creating another different object.

If you need to have some code in common between constructors you can place it in a separate method and call that method. Note that in the constructor you can call methods of the object but virtual methods are not going to dispatch do derived classes.

In other words if you have

struct Foo
{
    virtual void doit() { ... }
    Foo() {
        doit();
    }
};

struct Bar : Foo
{
    virtual void doit() { ... }
};

during the constructor of Bar the implementation of doit called will be the one defined in Foo because during the constructor of the base part of the derived object, the object is only a "base" object. Only at the end of the constructor it becomes a "derived" object right before executing any eventually present code in the "derived" constructor.

Be careful that other object oriented languages use a different approach...

For an explanation of what happens exactly in C++ see this article.

If you like instead a legalese description this is what is stated in the C++ standard at 12.7.4:

Member functions, including virtual functions (10.3), can be called during construction or destruction (12.6.2). When a virtual function is called directly or indirectly from a constructor (including from the mem-initializer for a data member) or from a destructor, and the object to which the call applies is the object under construction or destruction, the function called is the one defined in the constructor or destructor’s own class or in one of its bases, but not a function overriding it in a class derived from the constructor or destructor’s class, or overriding it in one of the other base classes of the most derived object (1.8). If the virtual function call uses an explicit class member access (5.2.5) and the object-expression refers to the object under construction or destruction but its type is neither the constructor or destructor’s own class or one of its bases, the result of the call is undefined.

6502
  • 112,025
  • 15
  • 165
  • 265
  • i will accept this because is most helpful :) quick and nasty hacks is what i like. – n00b May 17 '11 at 12:47
  • I think *"during the constructor of Bar the implementation of doit called will be the one defined in Foo"* is not entirely true. Does the Standard say that? Is it not unspecified/undefined? – Nawaz May 17 '11 at 13:03
  • @Nawaz: I can't find it in the standard but pdf searching sucks (and searching dead tree versions is even harder ;-) ). I added anyway a link to an explanation of the exact dynamic. I clearly remember reading about this from an authoritative source when I was studying C++ (but it was many years ago so I don't remember exactly what was the source... probably TCPPPL). – 6502 May 17 '11 at 17:14
0

First of all, the constructor is not just any method, you can't just use at as a mutator method.

that said, Am I right in thinking that your class had two fields, imie and wiek?

just do the init code again, this time with x.imie and x.wiek as input

Martin Kristiansen
  • 9,875
  • 10
  • 51
  • 83