tags:

views:

136

answers:

6

I am very new to C++ and still trying to learn syntax and best practices.

I've defined a method with a single parameter:

void foo(const std::string& name)

1) Is this a proper parameter declaration for a function that will be taking in a string defined by the user in, for example, a main method?

2) If this is proper/recommended syntax, what would an instantiation of a sample parameter look like?

+1  A: 

Is this a proper parameter declaration for a function that will be taking in a string defined by the user in, for example, a main method?

Yes.

#include <string>
using namespace std;
void foo(const string& name)
Nathan Campos
More organized? how so, all you have accomplished is adding std to the global namespace for the entire unit (or for any unit which includes this file).
Ed Swangren
It's just because the code gets more clean, in my opinion.
Nathan Campos
A: 

1) Yes, that's a very good way to do it if you only need to read the string in the function.
2) There is no instantiation going on?

henle
+8  A: 

Yes, that is the correct syntax. You can call it and provide parameters several different ways:

  • With a string literal:

    foo("bar");
    
  • With a string variable:

    std::string b = "bar";
    foo(b);
    
  • With the result of a function return type string:

    std::string quux();
    foo(quux());
    
  • With a char* variable:

    int main(int argc, char const* argv[]) {
      foo(argv[0]);
    }
    
Rob Kennedy
+1  A: 

1) It is a proper parameter declaration if function foo() doesn't mean to change the string. The 'const' keyword is used to signify that the string won't be changed by the receiver. If you write code in foo() which modifies the string you will get compiler error/warning.

2)

std::string theString = "Hello";
foo( theString );
sharkin
A: 

1) For most functions, it would be a fine signature. However, since you mentioned main(), there are only two valid signatures:

  • int main()
  • int main(int argc, const char* argv[])

...as you can see, you have to use C-style strings due to C-legacy compatibility (and efficiency)

2) Not sure I understand your second question, but since std::string has a constructor that takes a const char*, you can just say:

foo("hello");

or:

std::string input;
std::cout << "Enter some text:  ";
std::cin >> input;
foo(input);
Drew Hall
+2  A: 

I'm not sure if I fully understand your question, but I'll try to clarify it.

You use the terminology 'method'. I'm assuming that your method is encapsulated in a class? If so, then :-

In your header file (eg. source.h),

class dog
{
    ...
    public:
       void foo(const std::string &name);
    ...
};

In your source file (eg. source.cpp)

void dog::foo(const std::string &name)
{
    // Do something with 'name' in here
    std::string temp = name + " is OK!";
}

In your 'main' function, you can instantiate your 'dog' class, and call the 'foo' function like :-

void blah()
{
    dog my_class;
    my_class.foo("Testing my class");
}

If you want a function (ie. a 'method' that is not encapsulated within a class), then what you have is correct.

In your source file (eg. source.cpp)

void foo(const std::string &name)
{
    // Do something with 'name' in here
    std::string temp = name + " is OK!";
}

If you want to be able to call your function from outside that particular source file, you'll also need to forward declare your function in a header file.

In your header file (eg. source.h)

void foo(const std::string &name);

To call your function,

void blah()
{
    foo("Testing my class");
}

Hope this helps!

Darren Ford