const object and const member

J

Jess

Hello,

How can I create a constant object? I tried the following code but
failed:

#include<string>

using namespace std;

struct A{
int x;
string s;
};

int main(){
const A b;
b.x = 2;
b.s = "bs";
return 0;
}

In addition, how can I define a class/struct that has a const member
data? I tried the following but it also failed.

using namespace std;

struct A{
const int x;
string s;
};

Thanks.
 
G

Gianni Mariani

Jess said:
Hello,

How can I create a constant object? I tried the following code but
failed:

#include<string>

using namespace std;

struct A{
int x;
string s;
};

int main(){
const A b;
b.x = 2;
b.s = "bs";
return 0;
}

In addition, how can I define a class/struct that has a const member
data? I tried the following but it also failed.

using namespace std;

struct A{
const int x;
string s;
};

Thanks.

There are a number of things you can do ... I added some stuff to your
code below.

#include<string>

using namespace std;

struct A{
int x;
string s;
};

int main(){

// first way - initialize a non const object
A a;
a.x = 2;
a.s = "S";

// then initialize the class
const A c( a );

// second - use an initializer
const A b = { 2, "A" };

return 0;
}

namespace Way3
{
struct A{
const int x;
string s;

// have a constructor
A( int ia, string is ) : x( ia ), s( is ) {}
};

A a( 2, "S" );
const A b( 2, "S" );

// then initialize the class
const A c( a );

} // end namespace Way3
 
J

Juha Nieminen

Jess said:
How can I create a constant object? I tried the following code but
failed:
const A b;
b.x = 2;
b.s = "bs";

Since 'b' is const you naturally cannot modify it, as you are
trying to do there.

The only place where you can "modify" the contents of a constant
objects is in the constructor of that struct/class. So you'll have
to create a constructor for it and give those values as parameters
to the constructor.
In addition, how can I define a class/struct that has a const member
data? I tried the following but it also failed.

struct A{
const int x;
string s;
};

I see no problem with that code per se. Of course the only way
you can initialize 'x' is, once again, in the constructor of A,
so you'll have to create one. For example like this:

struct A
{
const int x;
string s;

A(int iVal = 0): x(iVal) {}
};
 
G

Gianni Mariani

Juha said:
... the only way
you can initialize 'x' is, once again, in the constructor of A,
so you'll have to create one. For example like this:

struct A
{
const int x;
string s;

A(int iVal = 0): x(iVal) {}
};

You don't "have to" create a constructor. There are other ways to
initialize it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top