C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Why is my const pointer not behaving?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
DamonChong
Guest





PostPosted: Sat Jan 29, 2005 5:56 pm    Post subject: Why is my const pointer not behaving? Reply with quote



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;
};

Back to top
Victor Bazarov
Guest





PostPosted: Sat Jan 29, 2005 6:24 pm    Post subject: Re: Why is my const pointer not behaving? Reply with quote



"DamonChong" <so_excited (AT) excite (DOT) com> wrote...
Quote:
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".

Quote:

[...]



Back to top
Arrivederci
Guest





PostPosted: Sat Jan 29, 2005 6:26 pm    Post subject: Re: Why is my const pointer not behaving? Reply with quote



DamonChong wrote:

Quote:
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

Back to top
Peter Koch Larsen
Guest





PostPosted: Sat Jan 29, 2005 6:30 pm    Post subject: Re: Why is my const pointer not behaving? Reply with quote

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_excited (AT) excite (DOT) com> skrev i en meddelelse
news:1107021400.925156.180400 (AT) z14g2000cwz (DOT) googlegroups.com...
Quote:
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 #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.

Quote:
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



Back to top
Shezan Baig
Guest





PostPosted: Sat Jan 29, 2005 10:07 pm    Post subject: Re: Why is my const pointer not behaving? Reply with quote


Arrivederci wrote:
Quote:
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-


Back to top
Adieu
Guest





PostPosted: Sun Jan 30, 2005 12:32 am    Post subject: Re: Why is my const pointer not behaving? Reply with quote

Shezan Baig wrote:

Quote:
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

Back to top
DamonChong
Guest





PostPosted: Sun Jan 30, 2005 2:02 am    Post subject: Re: Why is my const pointer not behaving? Reply with quote

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!

Back to top
Ron Natalie
Guest





PostPosted: Sun Jan 30, 2005 7:01 pm    Post subject: Re: Why is my const pointer not behaving? Reply with quote

Adieu wrote:

Quote:
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.


Back to top
Old Wolf
Guest





PostPosted: Sun Jan 30, 2005 7:32 pm    Post subject: Re: Why is my const pointer not behaving? Reply with quote

Peter Koch Larsen wrote:
Quote:
"DamonChong" <so_excited (AT) excite (DOT) 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).

Quote:
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.


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.