473,406 Members | 2,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

const object and const member

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.

Apr 25 '07 #1
3 1472
Jess wrote:
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
Apr 25 '07 #2
Jess wrote:
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) {}
};
Apr 25 '07 #3
Juha Nieminen wrote:
... 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.
Apr 25 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jim West | last post by:
Could someone please explain to me why the code segment class FOO { public: double *begin(); }; void bar(const FOO &foo) { foo.begin(); }
20
by: Corno | last post by:
Hi all, There's probably a good reason why a const object can call non const functions of the objects where it's member pointers point to. I just don't see it. For me, that makes the the const...
3
by: Zork | last post by:
Hi, I am a little confused with const functions, for instance (here i am just overloading the unary+ operator) if i define: 1) Length Length :: operator+ ( void ) const {return * this;} ... I...
7
by: Mark P | last post by:
The following compiles without error on four different platforms (Linux g++, Sun CC, HP aCC, Win Dev-C++) so I suspect it's ok, but I don't see why this isn't a const-related error. pB is a...
2
by: Lionel B | last post by:
I have a function which takes a functor argument. I which to call it for a functor which is actually a class member; this works fine, using the mem_fun_ref and bind1st functions (see listing 1...
5
by: John Goche | last post by:
Hello, I would like to know whethere there is a difference between a const variable and a static const variable inside a class. After all, if a variable is const in a class, the compiler can...
4
by: grizggg | last post by:
I have searched and not found an answer to this question. I ran upon the following statement in a *.cpp file in a member function: static const char * const pacz_HTMLContentTypeHeader =...
14
by: Wolfgang | last post by:
As I understand, a const member function is used by const object to ensure that its instance isn't modified throughout its life. Am I missing something.. #include <iostream> using namespace...
23
by: Kira Yamato | last post by:
It is erroneous to think that const objects will have constant behaviors too. Consider the following snip of code: class Person { public: Person(); string get_name() const
2
by: Angus | last post by:
I have a member function, int GetLogLevel() which I thought I should change to int GetLogLevel() const - I made the change and it works fine. But in the function I am creating buffers and of...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.