2
class example {
private:
    char Name[100];

public:
    example() { strcpy(Name, "no_name_yet"); }
    example(char n[100]) { strcpy(Name, n); }
};

int main() {
    example ex;
    char n[100];

    cout<<"Give name ";
    cin>>n;
    example();
}

I want to use the constructor with the parameter so that when the user gives a name it gets copied to the name variable. How can I use the constructor with the parameter instead of the default one? I tried

example(n)
example(char n)
example(*n)
example(n[100])

but none of them work.

sifferman
  • 2,955
  • 2
  • 27
  • 37
System
  • 241
  • 2
  • 4
  • 13
  • It should be pointed out to you, that as a parameter, `char n[100]` is identical to `char n[99]`, `char n[]` and `char * n`. That is to say, the actual parameter type is pointer to char, and the number is ignored. – Benjamin Lindley Dec 17 '11 at 19:32

2 Answers2

3

Easy:

#include <string>
#include <iostream>

class example {
 private:
    std::string name;

 public:
    example() : name("no name yet"){}
    example(std::string const& n) : name(n){}
};


int main() {
     example ex;
     std::string n;

     std::cout << "Give name ";
     std::cin >> n;
     example ex(n); // you have to give your instance a name, "ex" here
                    // and actually pass the contructor parameter
}
Xeo
  • 129,499
  • 52
  • 291
  • 397
  • As a general rule (at least in C++03), if you are going to copy the argument anyway, pass it by value. – Björn Pollex Dec 17 '11 at 18:29
  • @Björn: I might aswell do that and use `std::move` in the ctor, but y'know, I might also just keep it simple. :) – Xeo Dec 17 '11 at 18:30
  • 1
    @System: What exactly do you mean with that? You are only invoking the copy ctor of `std::string` here, which is unavoidable. – Xeo Dec 17 '11 at 18:31
  • i meant without the std::string, with the strcopy() – System Dec 17 '11 at 18:37
  • Why do you want to use char[100] and risk buffer overflow and make your life hard? – David Heffernan Dec 17 '11 at 18:39
  • @david if I use std::string can I set the size of the string? – System Dec 17 '11 at 18:43
  • @System: `strcopy` *is* a kind of copy contructor, for C style raw strings. It's also internally used by `std::string`. Also, `std::string` grows automatically. – Xeo Dec 17 '11 at 18:45
  • `std::string` is dynamically sized to fit its contents. Life is painful without it. – David Heffernan Dec 17 '11 at 18:47
  • @david when using std::string we can access the memory with a pointer? is the data stored in consecutive memory shells? – System Dec 17 '11 at 18:52
  • 1
    @System: It's best if you get a [good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), because those are basic questions about `std::string`. In general, the memory is consecutive. In the new standard (C++11), it's even promised. – Xeo Dec 17 '11 at 18:56
  • The string is stored in a contiguous block of memory. Why do you want to poke at it with pointers? – David Heffernan Dec 17 '11 at 18:56
  • @xeo I have Stroustrup's book but maybe it wasn't a very good choice for a beginner.. I am still at the beginning of it though.. – System Dec 17 '11 at 19:08
  • @System: Bjarne's book is a reference material rather than a teaching material. – Xeo Dec 17 '11 at 19:14
  • @David: `std::string::c_str()`, for C interfacing. – Xeo Dec 17 '11 at 19:15
2

It is example my_instance_of_example(n).

I must note, however, that using char arrays for strings is not what you do in C++. You should use std::string instead, it gives you a lot more flexibility.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523