MudBytes
» MUDBytes Community » Language Discussions » C and C++ » proper usage of references
Pages: << prev 1 next >>
proper usage of references
JohnnyStarr
Wizard






Group: Members
Posts: 953
Joined: Feb 14, 2009

Go to the bottom of the page Go to the top of the page
#1 id:47624 Posted Jun 24, 2010, 8:20 pm

This may be a simple question, but the examples in my C++ book actually don't include the specifics of what I'm wondering.
It seems that when you create a class, and want to embed that into another custom class, it is a bit more complicated using
a reference than a pointer. If I wanted to add an STL object like a vector or list, I could just do it this way:

Code (C++):
1
2
3
4
5
6
7
 
class Interpreter {
    std::vector<int> dumbList;
};
 


But say I wanted each Interpreter to have a Lexer and Parser object? How would I go about that? Would it make more sense
to instantiate the objects first, then in the constructor send them by ref? Or, could I initialize them within the constructor and
assign them afterwards? I know I could just use a pointer, but it doesn't make much sense considering that the Lexer and
Parser objects will never change, and therefor the variable representing them shouldn't change as well. I'll throw this out there
too (since it's almost on topic).

Strings in C++ elude me a bit as well. If I were to have a object constructor take a string literal as a parameter, I would assume that it would be received as a string. But whenever I do that, it says "cannot convert const char* to std::string". So,
I understand the whole C-Legacy deal, but this makes it a bit cumbersome. Must one create a string object and then pass it
by reference just to appease the C++ gods? I've observed that I could overload the constructor and receive a const char* which
could call the std::string constructor and convert, but that seems a bit awkward.

Last edited Jun 24, 2010, 8:20 pm by JohnnyStarr
JohnnyStarr
Wizard






Group: Members
Posts: 953
Joined: Feb 14, 2009

Go to the bottom of the page Go to the top of the page
#2 id:47663 Posted Jun 25, 2010, 9:16 am

weird, my post didn't show up on the home page  :cry:

3squire
Magician




Group: Members
Posts: 63
Joined: Mar 22, 2010

Go to the bottom of the page Go to the top of the page
#3 id:47664 Posted Jun 25, 2010, 10:29 am

It did, no worries. Kiasyn ####ed up everybody's version of the homepage with his upgrade -- You have to go to  http://www.mudbytes.net/index.php?a=recent then at the bottom of the left column are two buttons "update" and "save" -- click save with all the boxes checked, and you will see the latest post in every forum.

ocson
Fledgling




Group: Members
Posts: 7
Joined: Aug 28, 2009

Go to the bottom of the page Go to the top of the page
#4 id:47665 Posted Jun 25, 2010, 10:32 am

JohnnyStarr said:
weird, my post didn't show up on the home page  :cry:


It's there now, well at least it is for me.

You can use the initializer list:

Code (c++):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 
class Interpreter
{
    public:
 
    Interpreter();
 
    private:
 
    std::vector<int> dumbList;
    Lexer lexer;
    Parser parser;
};
 
Interpreter::Interpreter() : lexer(lcparam1,lcparam2,...), parser(pcparam1,pcparam2,...)
{
}
 
 


Where the lcparams and the pcparams are constructor parameters for the lexer and parser.  Then you don't have to worry about references or pointers.

For the strings, if your constructor accepts a string by value or (better) constant reference, it should convert just fine.  If your constructor accepts a non-constant string reference, you may have to explictly create a string object.

kiasyn
Wizard






Group: Administrators
Posts: 1,109
Joined: May 15, 2006

Go to the bottom of the page Go to the top of the page
#5 id:47675 Posted Jun 25, 2010, 3:44 pm


3squire said:
It did, no worries. Kiasyn ####ed up everybody's version of the homepage with his upgrade -- You have to go to  http://www.mudbytes.net/index.php?a=recent then at the bottom of the left column are two buttons "update" and "save" -- click save with all the boxes checked, and you will see the latest post in every forum.


that was davion's upgrade, and theres really no need to be rude about it
.........................
http://www.mudbytes.net/kiasyn-sig.png

http://portal.hypernia.com/banners/hypernia_button.jpg http://www.shastaherps.org/badges/linode88.png

JohnnyStarr
Wizard






Group: Members
Posts: 953
Joined: Feb 14, 2009

Go to the bottom of the page Go to the top of the page
#6 id:47787 Posted Jun 28, 2010, 4:22 pm

Thanks guys, very helpful.

I tried to compile my ROM project with g++ 4.4.3, which worked fine in 4.1* but it's giving me this warning all over the place:

warning: deprecated conversion from string constant to ‘char*’

Which I know has to do with the fact that the string literals are being stored or passed as a char* string and not a const char* right?
I really hope thats all, because I don't want to change it all to std::string. It compiles with no errors, so maybe it's just a matter of
passing a no warning arg to gcc, but I'm curious what others think about the situation.

Last edited Jun 28, 2010, 4:23 pm by JohnnyStarr
David Haley
Wizard






Group: Members
Posts: 7,783
Joined: Jun 30, 2007

Go to the bottom of the page Go to the top of the page
#7 id:47790 Posted Jun 28, 2010, 8:22 pm

It's better to fix those warnings but not (usually) necessary. In certain circumstances, you can get a crash, such as when a read-only string in the data segment (that should be const) is modified.
.........................
-- d.c.h --
BabbleMUD Project (custom codebase)
Legends of the Darkstone (head coder)
http://david.the-haleys.org
.........................

Runter
Wizard






Group: Developer
Posts: 2,929
Joined: Jun 1, 2006

Go to the bottom of the page Go to the top of the page
#8 id:47791 Posted Jun 28, 2010, 8:58 pm

DH said:
It's better to fix those warnings but not (usually) necessary


Kinda depends on how you fix it though, right?  I know you get it, but probably some people out there don't.  I guess my point is just making a warning go away with obligatory code that effectively does the same thing doesn't make much difference.  Imo if you're being warned about something it's probably because you're doing something poorly thought out in the first place.  Instead of focusing on making warnings go away we should probably focus on what they're trying to warn us about. 

Code (C):
1
2
3
4
5
6
 
int i = 10;
char *c = (char*) i; // fixed
 


That being said.  Warnings aren't compiler errors.  I typically don't mind having warnings in a project.

.........................
CoralMud project (Mud codebase in Ruby)
HaikuMud project (HTML/JS/Ruby, web-app based MMO codebase)
My Web CV

David Haley
Wizard






Group: Members
Posts: 7,783
Joined: Jun 30, 2007

Go to the bottom of the page Go to the top of the page
#9 id:47792 Posted Jun 28, 2010, 9:07 pm

Hmm, yes. I meant actually fixing the warning by not masking it, but making functions that should take const char* actually take const char*, and not casting a const char* to a char* but working around it as necessary (creating temporary copies if needed). It's virtually never a good idea to just mask a warning unless you really know what you're doing.

This particular warning is just annoying because lots of code violates the principle, and it was accepted before (sometimes causing crashes). So a lot of people disable it; there's a compiler flag that does it. (-no-const-literals?? don't remember)
.........................
-- d.c.h --
BabbleMUD Project (custom codebase)
Legends of the Darkstone (head coder)
http://david.the-haleys.org
.........................

3squire
Magician




Group: Members
Posts: 63
Joined: Mar 22, 2010

Go to the bottom of the page Go to the top of the page
#10 id:47794 Posted Jun 29, 2010, 12:11 am


kiasyn said:

3squire said:
It did, no worries. Kiasyn ####ed up everybody's version of the homepage with his upgrade -- You have to go to  http://www.mudbytes.net/index.php?a=recent then at the bottom of the left column are two buttons "update" and "save" -- click save with all the boxes checked, and you will see the latest post in every forum.


that was davion's upgrade, and theres really no need to be rude about it


True. I apologize, both for both mistakes.

Kaz
Conjurer






Group: Members
Posts: 125
Joined: Oct 31, 2009

Go to the bottom of the page Go to the top of the page
#11 id:47797 Posted Jun 29, 2010, 1:19 am

JohnnyStarr said:
Strings in C++ elude me a bit as well. If I were to have a object constructor take a string literal as a parameter, I would assume that it would be received as a string. But whenever I do that, it says "cannot convert const char* to std::string". So, I understand the whole C-Legacy deal, but this makes it a bit cumbersome. Must one create a string object and then pass it by reference just to appease the C++ gods? I've observed that I could overload the constructor and receive a const char* which could call the std::string constructor and convert, but that seems a bit awkward.


A string literal in C++ is the same type of C.  That is, the type of "Hello, world", is const char *.

There exist implicit conversions to make this less painful.  For example, you can have a function that take a reference to a constant std::string, and you can pass std::strings, char*s, and string literals into it.

Code (C++):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <string>
void fn(const std::string &foo);
 
int main()
{
    fn("Hello, world!");
 
    char *charstarfoo = "Hello, world!";
 
    fn(charstarfoo);
 
    std::string stdstringfoo = "Hello, world!";
 
    fn(stdstringfoo);
}


There is no such implicit conversion the other way, but std::string objects have a c_str() member function that returns the const char* version of that string:

Code (C++):
1
2
3
4
5
6
7
8
9
10
11
#include <string>
void fn(const char *foo);
 
int main()
{
    std::string stdstringfoo = "Hello, world!";
 
    fn(stdstringfoo.c_str());
}


In short.  Yes, you must appease the C++ gods, one way or another.  std::string is just another object and has no special status in the language.

Pages:<< prev 1 next >>

Valid XHTML 1.1! Valid CSS!