0

I have a lib.h, lib.cpp and test.cpp. I would like to ask what is better? lib.h

class c
{
    std::string name;
}*cc;

lib.cpp

{
    std::cout << "the name is:" << name << std:: endl;
}

test.cpp

main()
{ 
    c tst;
    tst.name="diana";
}

What should I use? std::string name or std::string *name? How can i work with &name, how will my code change and which one of these 2 methods is the best one?

beduin
  • 7,943
  • 3
  • 27
  • 24
linuxx
  • 1,487
  • 4
  • 16
  • 17
  • 1
    Edited the code mess: Coding style matters. If you don't bother to even format your code, you cannot hope for sudden '_understanding_' to happen miraculously – sehe Apr 29 '11 at 12:54
  • where am i wrong in the eq code i've posted? – linuxx Apr 29 '11 at 12:59
  • Don't believe ubuntu tag really belong here. Removed it. – beduin Apr 29 '11 at 13:00
  • I just cleaned it up. Indents, spacing, namespace qualifiers. Nothing was 'wrong', it was just not very readable, and unreadable code _will_ cause you to make more bugs than you can fix (I bet you hadn't even realized, you were declaring a variable `cc` as a pointer to `c`?) – sehe Apr 29 '11 at 13:02
  • see my answer. Your code actually has an error. In your `main` function you try to modify private member `name`. – beduin Apr 29 '11 at 13:06

5 Answers5

3

std::string & name; is a reference. You'll need to initialize it in the constructor, so it actually refers to something. What it refers to must exist as long as your c instance exists. It's not the right solution for this.

std::string name; is an actual variable, use this.

Erik
  • 88,732
  • 13
  • 198
  • 189
  • what about std::string *name;? – linuxx Apr 29 '11 at 13:00
  • This would be the pointer to string. Find more about differences between pointers and references [here](http://stackoverflow.com/questions/57483/what-are-the-differences-between-pointer-variable-and-reference-variable-in-c) – beduin Apr 29 '11 at 13:04
3

First, I hardly believe your code will compile while in your main you try to get access to private data member name.

About &. It is really hard to define where to start. In short std::string &name is called reference to object of type std::string. Reference is somehow an alias to some other object. The main feature is, that you always have to initialize refence to object while creating and you can't reinitialize reference to point to another object. More about this feature you can read in C++ FAQ

EDIT Generally you can declare public, protected and private members of your class in with arbitrary ordering:

class MyClass {
//here goes private members when working with class
//and public when working with structs

public:
//here goes public members
protected:
//here goes protected
private: 
//here goes private
public: 
//here goes public again
};

Ordering of members declaration is actually code policy question. For example, google style guide recommends to put all public members before private.

About hiding function members (not necessary private). You actually can't hide function member declaration, but there are several ways to "hide" implementation, but I am not sure that it's the definition of hiding you are talking about. But you can check PImpl idiom. This requires understanding of pointers so I advice you to start with them first.

Small code sample for working with pointer to string:

#include <iostream>
#include <string>

class MyClass {
public:
 std::string *pstr;
};

int main() {
 std::string str("test");
 MyClass myObj;

 myObj.pstr = &str;
 std::cout << myObj.pstr->c_str()  << std::endl;
}
seaotternerd
  • 6,298
  • 2
  • 47
  • 58
beduin
  • 7,943
  • 3
  • 27
  • 24
  • thx for your answer. WHat about std::string *name;? And one more thing? If I add public to my class from .h file how can I define a private method called bool print(std::string name)? Is there a way define and work with the method without seing it in the .h file? because it's a private method i want to find a solution to not puting it in the .h – linuxx Apr 29 '11 at 13:05
  • See my reedited post for your question about public/private declaration. About `std::string *name` - this is called a pointer to string and more about differences between pointers and references you can find [here](http://stackoverflow.com/questions/57483/what-are-the-differences-between-pointer-variable-and-reference-variable-in-c). – beduin Apr 29 '11 at 13:16
  • and if i want MyClass to be public how to do it? public: class MyClass{.....}*myclass? – linuxx Apr 29 '11 at 13:20
  • One more thing. In the header file I don't want to be seen the private methods :). I mean I don't want to have private: method(int x); – linuxx Apr 29 '11 at 13:21
  • No, it's right syntax. In this case you can write `class MyClass {public: /**/};`. But if you want to have the whole class public consider using `struct` instead of class. `struct` members are public by default. – beduin Apr 29 '11 at 13:23
  • can you please reeditate my code using in the header std::string *name. How could I call this from main?Please add a private method that is not declared in the .h file. Just in the .cpp and give me a test in main.cpp.thx – linuxx Apr 29 '11 at 13:24
  • You can't actually "vanish" your private members. Somehow you can do this with PIpml idiom (see answer) but you still will ba able to see member declarations. – beduin Apr 29 '11 at 13:28
  • thx one more time. Besides std::string str("test") can I use std::string str="test";? – linuxx Apr 29 '11 at 13:52
  • @linuxx: yes. you can. But when using `std::string("test")` you build string object with call to string constructor while in second case you are using temporary string object (one constructor call) and assignment operator. So this is mostly just performance isue. Also it should be noted that modern compilers often optimize second case to first. – beduin Apr 29 '11 at 14:37
2

std::string &name is "only" a reference to a string (a bit like a pointer). Because the name definitely belongs to the class c, I think it would make sense to have an instance there.

References are put to good use when passing instances around (to avoid copying).

Delphinator
  • 513
  • 3
  • 11
1
const std::string& 

is reference to a std::string, it is very important to understand the implications of that with respect to the lifetime of variables. Once the referenced variable goes away, the reference is no longer valid, and this is a very common way to f*ck up for beginning C++ programmers.

YMMV, Pick up a good tutorial first, and meanwhile, don't use references unless you know why and what you're doing

Good luck

sehe
  • 374,641
  • 47
  • 450
  • 633
0

I'd use: string name; because string* name is just a pointer that needs to be given a value, and then I'd have to think of a way to clean it up later all by myself, and string& name, would be just a name that again has to be initialized.

atoMerz
  • 7,534
  • 16
  • 61
  • 101