What does the clone_prefix used for?

I am studying coding by watching videos. The guy has the keyword clone_prefix in his code.
I am wondering what is that used for since the guy did not explain it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

 //...

 //data members
  // string _type = "";
  //  string _name = "";
   // string _sound = "";


//implementation

//...

Animal::Animal(const Animal & rhs) {
    puts("copy constructor");
    _name = clone_prefix + rhs._name;          // <---- here
    _type = rhs._type;
    _sound = rhs._sound;
}
Last edited on
> The guy has the keyword clone_prefix in his code.

It is not a keyword; it is the name of a variable (which denotes a sequence of characters).


> I am wondering what is that used for

When a copy of an animal is made, the name in the copy is prefixed with the characters in clone_prefix

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <string>

struct animal
{
    animal( std::string type, std::string name, std::string sound )
        : type_(type), name_(name), sound_(sound) {}

    animal( const animal& that )
        : type_( that.type() ), name_( clone_prefix + that.name() ), sound_( that.sound() ) {}

    const std::string& name() const noexcept { return name_ ; }
    const std::string& type() const noexcept { return type_ ; }
    const std::string& sound() const noexcept { return sound_ ; }

    private:

        std::string type_ ;
        std::string name_ ;
        std::string sound_ ;

        static constexpr char clone_prefix[] = "(clone of) " ;

    friend std::ostream& operator<< ( std::ostream& stm, const animal& a )
    { return stm << "animal{ " << a.type() << ", " << a.name() << ", " << a.sound() << "! }" ; }
};

int main()
{
    const animal fido( "dog", "fido", "woof" ) ;
    std::cout << fido << '\n' ; // animal{ dog, fido, woof! }

    const animal clone_of_fido(fido) ;
    std::cout << clone_of_fido << '\n' ; // animal{ dog, (clone of) fido, woof! }
}

http://coliru.stacked-crooked.com/a/08b0b4c552989441
Oh, ok lol, it is a variable.. It looked like a keyword to me at first lol.. Thanks for your explanation.



Problem Solved.
Last edited on
Topic archived. No new replies allowed.