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?

5
  • 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 Commented Apr 29, 2011 at 12:54
  • where am i wrong in the eq code i've posted? Commented Apr 29, 2011 at 12:59
  • Don't believe ubuntu tag really belong here. Removed it. Commented Apr 29, 2011 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?) Commented Apr 29, 2011 at 13:02
  • see my answer. Your code actually has an error. In your main function you try to modify private member name. Commented Apr 29, 2011 at 13:06

5 Answers 5

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;
}
Sign up to request clarification or add additional context in comments.

9 Comments

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
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.
and if i want MyClass to be public how to do it? public: class MyClass{.....}*myclass?
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);
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.
|
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.

2 Comments

what about std::string *name;?
This would be the pointer to string. Find more about differences between pointers and references here
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).

1 Comment

duplication? you mean, copying
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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.