By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,131 Members | 2,238 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,131 IT Pros & Developers. It's quick & easy.

Why is my const pointer not behaving?

P: n/a
Hi, I am new and had this piece of code which I created but it is not
behaving as expected. I'm using g++, can someone help me out? Thank
you.

//--------file (Object.cc)-----------
class Object {
public:
Object(const char * c){
id = c;
}

~Object() {}

const char * get_id() const { return id; }

private:
const char * id;
};

//-------file (Test.cc)-------------
#include <iostream>
#include <sstream>
#include "Object.cc"

class Test{
public:
Object ** obj;
Test(){
obj = new Object * [3];
for( int i=0; i<3; i++ ){
std::ostringstream os;
os << i;
obj[i] = new Object( os.str().c_str() );
std::cout << "Created id = " << obj[i]->get_id() <<
std::endl;
}
}

~Test(){}
};

int main() {
Test t;
for(int i=0; i<3; i++){
std::cout << "Object id - " << t.obj[i]->get_id()
<< " // expecting " << i << std::endl;
}
}

//----------------output upon running------------
=> ./Test
Created id = 0
Created id = 1
Created id = 2
Object id - 2 // expecting 0
Object id - 2 // expecting 1
Object id - 2 // expecting 2

My query is if I changed the "const char *" to simply int as the
parameter to pass the Object constructor like this and make the
appropriate changes, it works as expected. Now, I know i'm missing
something here!

//---------------file (Object1.cc)-----------------
class Object1 {
public:
Object1(int c){
id = c;
}

~Object1() {}
const int get_id() const { return id; }

private:
int id;
};

Jul 23 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
"DamonChong" <so********@excite.com> wrote...
Hi, I am new and had this piece of code which I created but it is not
behaving as expected. I'm using g++, can someone help me out? Thank
you.
Your 'Object' class is holding onto a pointer that quickly becomes
invalid. You need to rethink what you store in 'Object' and why. I
recommend making 'id' a "std::string".

[...]

Jul 23 '05 #2

P: n/a
DamonChong wrote:
Hi, I am new and had this piece of code which I created but it is not
behaving as expected. I'm using g++, can someone help me out? Thank
you.


const pointer?
char const *id;

http://www.possibility.com/Cpp/const.html
Jul 23 '05 #3

P: n/a
I've already advised you not to use pointers. They really should be used
very carefully, and not by beginners. Here your class "Object" should have a
std::string as a member: thus id should be a std::string, not a pointer to
char.
"DamonChong" <so********@excite.com> skrev i en meddelelse
news:11**********************@z14g2000cwz.googlegr oups.com...
Hi, I am new and had this piece of code which I created but it is not
behaving as expected. I'm using g++, can someone help me out? Thank
you.

//--------file (Object.cc)-----------
class Object {
public:
Object(const char * c){
id = c;
}

~Object() {}

const char * get_id() const { return id; }

private:
const char * id;
};

//-------file (Test.cc)-------------
#include <iostream>
#include <sstream>
#include "Object.cc"

class Test{
public:
Object ** obj;
Test(){
obj = new Object * [3];
for( int i=0; i<3; i++ ){
std::ostringstream os;
os << i;
obj[i] = new Object( os.str().c_str() ); os.str().c_str() is only valid as long as os is not changed. Thus you keep
tenporary pointers in your newly created object.
std::cout << "Created id = " << obj[i]->get_id() <<
std::endl;
}
}

~Test(){}
};

int main() {
Test t;
for(int i=0; i<3; i++){
std::cout << "Object id - " << t.obj[i]->get_id()
<< " // expecting " << i << std::endl;
}
}

//----------------output upon running------------
=> ./Test
Created id = 0
Created id = 1
Created id = 2
Object id - 2 // expecting 0
Object id - 2 // expecting 1
Object id - 2 // expecting 2

My query is if I changed the "const char *" to simply int as the
parameter to pass the Object constructor like this and make the
appropriate changes, it works as expected. Now, I know i'm missing
something here!


You are missing a good book - perhaps "Accelerated C++", of which I've read
the first chapter. That chapter is very good and the book as a whole has
been praised by all.
[snip]

/Peter
Jul 23 '05 #4

P: n/a

Arrivederci wrote:
DamonChong wrote:
Hi, I am new and had this piece of code which I created but it is not behaving as expected. I'm using g++, can someone help me out? Thank
you.


const pointer?
char const *id;

http://www.possibility.com/Cpp/const.html


It's a nice article, but I'm not sure of its relevance to the OP's
question.

-shez-

Jul 23 '05 #5

P: n/a
Shezan Baig wrote:
Arrivederci wrote:
DamonChong wrote:
Hi, I am new and had this piece of code which I created but it is not behaving as expected. I'm using g++, can someone help me out? Thank
you.


const pointer?
char const *id;

http://www.possibility.com/Cpp/const.html


It's a nice article, but I'm not sure of its relevance to the OP's
question.


He's trying to understand constness. That's why he's playing.

--

I tell you, we are here on Earth to fart around, and don't let anybody tell
you different. -- Kurt Vonnegut
Jul 23 '05 #6

P: n/a
Ah, now i see. Kind of silly, I bought the book but lend it to someone
instead. Will get it back and go through it, thanks again!

Jul 23 '05 #7

P: n/a
Adieu wrote:
He's trying to understand constness. That's why he's playing.


The problem is that there is little runtime influence to const.
const like access control is a compile time issue. If it compiles
then the checking has been done. Assuming the compiler actually
enforces constness at the runtime can't be determine without undefined
behavior.

Jul 23 '05 #8

P: n/a
Peter Koch Larsen wrote:
"DamonChong" <so********@excite.com> skrev:
Object ** obj;
Test(){
obj = new Object * [3]; for( int i=0; i<3; i++ ){
std::ostringstream os;
os << i;
obj[i] = new Object( os.str().c_str() );

All of that guff could have been replaced with
Object obj[3]. Or if dynamic sizing is needed,
use a vector. (Assuming Object is copyable).
os.str().c_str() is only valid as long as os is not changed.


It's only valid until the end of the full-expression it
occurs in. But even if you had an implementation where
c_str() was valid until its string changed, os.str()
returns a temporary string anyway, which is destroyed
at the end of the full-expression.

Jul 23 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.