0

I am trying to write a constructor in C++ (I am new).

My Attempt:

class Tree
{
    private:
        int leaf;

    public:
        Tree(int leaf); //constructor
};

Tree::Tree(int leaf) //constructor
{
strcpy(this->leaf, leaf);
}

Is this the proper way how to do it? Since I found many different version with srcpy, without , etc.

user3126119
  • 323
  • 1
  • 3
  • 10
  • possible duplicate of [Initializing fields in constructor - initializer list vs constructor body](http://stackoverflow.com/questions/9903248/initializing-fields-in-constructor-initializer-list-vs-constructor-body) – ManuelH Jan 05 '14 at 21:35
  • 6
    Would you kindly do us a favour and help us understand how people learn C++ -- please explain *why* did you feel that `strcpy` was appropriate here? I'm genuinely curious about the thought process. – Kerrek SB Jan 05 '14 at 21:37
  • 1
    I'm sorry to see that you did not read [my answer on your previous question](http://stackoverflow.com/a/20939216/560648), or Google `strcpy` to find out what it does. – Lightness Races in Orbit Jan 05 '14 at 22:03
  • @LightnessRacesinOrbit "What does this code do? It burns the eyes of children." Hahaha – Proxy Jan 05 '14 at 22:56

3 Answers3

7

No, it is not. strcpy is for copying null-terminated strings. Use the constructor initialization list:

Tree::Tree(int leaf) : leaf(leaf) {}

Also note that your constructor allows for implicit conversions from int to Tree. So you can do this kind of thing:

Tree t = 4 + 5;

If you do not want this behaviour, mark the constructor explicit:

explicit Tree(int leaf);
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
1

You can simply initialize the int like this:

Tree::Tree(int leaf) //constructor
  : leaf(leaf)
{
}

strcpy is not needed, it is meant for C-strings. It does not compile since it expects a char* pointer.

leemes
  • 44,967
  • 21
  • 135
  • 183
cageman
  • 333
  • 2
  • 10
  • so if you use a string (char* ) you have to use strcpy then? – user3126119 Jan 05 '14 at 21:37
  • @user3126119, I suggest using a `std::string` and doing the exact same thing as this. @cage, It initializes it, not assigns. – chris Jan 05 '14 at 21:43
  • Do yourself a favor and don't use C-strings (char*). Use C++ strings (`std::string`) which can be initialized like your `int`. – leemes Jan 05 '14 at 21:44
  • @cageman it's initialization, not assignment. Also, `strcpy` [won't compile](http://ideone.com/teActm) here, fixed that in your answer. – leemes Jan 05 '14 at 21:47
  • can you edit my code with std::string since I cannot figure how to do it I have been trying and still no success. I would appreciate a lot – user3126119 Jan 05 '14 at 22:44
0

You can use std::copy if you really want to use some sort of memory-copying function, but m_leaf = leaf works just as well and is easier to read. Notice my use of m_leaf instead of leaf - it's common to add the prefix m_ before all member variables (More on Hungarian Notation here).

Proxy
  • 1,824
  • 1
  • 16
  • 27