473,387 Members | 1,904 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,387 software developers and data experts.

data members

data members defined after a private label are not accessible to code
that uses the class.
So, can I define the private data members as const?

Nov 30 '06 #1
10 2059
asdf wrote:
data members defined after a private label are not accessible to code
that uses the class.
So, can I define the private data members as const?
Have you tried it? Why do you think you could not?

--
Ian Collins.
Nov 30 '06 #2
* asdf:
data members defined after a private label are not accessible to code
that uses the class.
You seem to have the right idea, roughly, but consider:

* What if after a "private:" I put a "public:" and some data member
declarations? Then they are certainly /after/ the "private:".

* What if the code that uses the class is in member functions of
the class itself?

So, can I define the private data members as const?
Yes. But generally you should only use const members for a class that's
designed to be non-copyable (less imprecisely: for a class whose
instances are non-copyable by design). Using a const or reference
member prevents ordinary assignment (the member can not be changed) but
not copy construction (the member can be copied to a new instance), and
since that's not entirely intuitive to all, it can be surprising and
thus lead to bugs.

So the simple answer is, to keep things simple, don't use const or
reference members.

On the other hand it can often be a good idea to disable copying, which
you can do (for client code) by declaring a private copy constructor and
a private copy assignment operator, like

class Foo
{
private:
Foo( Foo const& ); // No such.
Foo& operator=( Foo const& ); // No such.
public:
// Whatever.
};

Disabling copying -- non-copyable class -- removes problems with
e.g. copying handles or dynamically allocated objects "owned" by each
instance.

But it has problems of its own, e.g. with current C++, C++2003, it
prohibits passing a temporary as actual argument to a 'Foo const&'
formal argument.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 30 '06 #3
I see, but my idea is quite simple, may I define:

class Foo
{
const int x;
public:
Foo();
};

?

Alf P. Steinbach wrote:
* asdf:
data members defined after a private label are not accessible to code
that uses the class.

You seem to have the right idea, roughly, but consider:

* What if after a "private:" I put a "public:" and some data member
declarations? Then they are certainly /after/ the "private:".

* What if the code that uses the class is in member functions of
the class itself?

So, can I define the private data members as const?

Yes. But generally you should only use const members for a class that's
designed to be non-copyable (less imprecisely: for a class whose
instances are non-copyable by design). Using a const or reference
member prevents ordinary assignment (the member can not be changed) but
not copy construction (the member can be copied to a new instance), and
since that's not entirely intuitive to all, it can be surprising and
thus lead to bugs.

So the simple answer is, to keep things simple, don't use const or
reference members.

On the other hand it can often be a good idea to disable copying, which
you can do (for client code) by declaring a private copy constructor and
a private copy assignment operator, like

class Foo
{
private:
Foo( Foo const& ); // No such.
Foo& operator=( Foo const& ); // No such.
public:
// Whatever.
};

Disabling copying -- non-copyable class -- removes problems with
e.g. copying handles or dynamically allocated objects "owned" by each
instance.

But it has problems of its own, e.g. with current C++, C++2003, it
prohibits passing a temporary as actual argument to a 'Foo const&'
formal argument.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 30 '06 #4
Foo
{
private:
const int x;
};

error : no appropriate default constructor available

I am curious about this error, I know the constant data must be defined
(initialized).
I didn't define the constructor explicitly, but the default
constructor, which is automatically generated by the compiler, should
work.

Thanks.

Ian Collins wrote:
asdf wrote:
data members defined after a private label are not accessible to code
that uses the class.
So, can I define the private data members as const?
Have you tried it? Why do you think you could not?

--
Ian Collins.
Nov 30 '06 #5
asdf wrote:

Please don't top-post.
Ian Collins wrote:
>>asdf wrote:
>>>data members defined after a private label are not accessible to code
that uses the class.
So, can I define the private data members as const?

Have you tried it? Why do you think you could not?
Foo
{
private:
const int x;
};

error : no appropriate default constructor available

I am curious about this error, I know the constant data must be defined
(initialized).
I didn't define the constructor explicitly, but the default
constructor, which is automatically generated by the compiler, should
work.
You must initialise a const member from an initialiser in the
constructor. There isn't another way to assign a value to a const member.

Foo
{
private:
const int x;

public:

Foo() : x(42) {}
};

--
Ian Collins.
Nov 30 '06 #6
asdf wrote:
Foo
{
private:
const int x;
};

error : no appropriate default constructor available

I am curious about this error, I know the constant data must be defined
(initialized).
I didn't define the constructor explicitly, but the default
constructor, which is automatically generated by the compiler, should
work.
Since x is constant, you have to initialize it. Since it's a class
member, that means you have to use member initialization syntax in the
constructor.

Try:

class Foo
{
private:

const int x;

public:

// default ctor
Foo() : x(0) {}

// parameterized ctor
explicit Foo(int x) : x(x) {}
};

If I'm not mistaken, you can leave the 0 out of the initializer, because
a default-initialized int is equal to 0. Someone please correct me if
I'm wrong here.

Nate
Nov 30 '06 #7
Thanks. My question is, since I didn't define any constructor, the
compiler will define the default constructor, which could initialize
the data member in the default way.

I don't know why the default constructor doesn't work.

Ian Collins wrote:
asdf wrote:

Please don't top-post.
Ian Collins wrote:
>asdf wrote:

data members defined after a private label are not accessible to code
that uses the class.
So, can I define the private data members as const?
Have you tried it? Why do you think you could not?
Foo
{
private:
const int x;
};

error : no appropriate default constructor available

I am curious about this error, I know the constant data must be defined
(initialized).
I didn't define the constructor explicitly, but the default
constructor, which is automatically generated by the compiler, should
work.
You must initialise a const member from an initialiser in the
constructor. There isn't another way to assign a value to a const member.

Foo
{
private:
const int x;

public:

Foo() : x(42) {}
};

--
Ian Collins.
Nov 30 '06 #8
asdf wrote:
Thanks. My question is, since I didn't define any constructor, the
compiler will define the default constructor, which could initialize
the data member in the default way.

I don't know why the default constructor doesn't work.
Please don't top post!

--
Ian Collins.
Nov 30 '06 #9
First of, don't top-post! To make my point I'll quote Alf's signature
which demonstrates why top-posting is bad.
Alf P. Steinbach wrote:
>A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

I see, but my idea is quite simple, may I define:

class Foo
{
const int x;
public:
Foo();
};
Yes, but whether it will work or not depends on what more code you have.
You have declared the Foo-constructor but not defined it, and it's the
definition of the constructor that determines if the code will work or
not. If you define it something like this:

Foo::Foo()
{ }

Then it will not work since the member x has not been initialized, the
compiler won't do this for you since it does not know what to initialize
x to, so you need:

Foo::Foo()
: x()
{ }

at the very least, more useful would be

Foo::Foo(int xin)
: x(xin)
{}

--
Erik Wikström
Nov 30 '06 #10
asdf wrote:
I see, but my idea is quite simple, may I define:
Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the group FAQ list:
<http://www.parashift.com/c++-faq-lite/how-to-post.html>
Nov 30 '06 #11

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

Similar topics

5
by: Suzanne Vogel | last post by:
Hi, Given: I have a class with protected or private data members, some of them without accessor methods. It's someone else's class, so I can't change it. (eg, I can't add accessor methods to the...
2
by: Tim | last post by:
Please advise if you can. Presumably initialisation of members in member initialisation lists is perfomed by 'C' run-time startup. If the CRT was never started-up would those members be garbage?...
9
by: Anon Email | last post by:
Hi people, I'm learning about header files in C++. The following is code from Bartosz Milewski: // Code const int maxStack = 16; class IStack
8
by: Scott J. McCaughrin | last post by:
The following program compiles fine but elicits this message from the linker: "undefined reference to VarArray::funct" and thus fails. It seems to behave as if the static data-member:...
10
by: Zap | last post by:
Widespread opinion is that public data members are evil, because if you have to change the way the data is stored in your class you have to break the code accessing it, etc. After reading this...
6
by: lovecreatesbeauty | last post by:
Hello Experts, Why static data members can be declared as the type of class which it belongs to? Inside a class, non-static data members such as pointers and references can be declared as...
3
by: ectoplasm | last post by:
I'd like to ask for advice on the following. I have a data structure of network elements ('NetworkElement' base class & derived classes like Server, Area, Cell, etc.) The network elements are...
1
by: Nathan Sokalski | last post by:
I have retrieved data from a database using a SELECT statement that includes an INNER JOIN. The data seems to be retrieved to the DataSet OK, but I am having trouble getting the data from the...
2
by: Jason | last post by:
Hello: First, if this is one of those "questions asked a million times" just say so and I'll dig a little deeper. If not, then... I'm curious, is it typical to use member data (properties,...
18
by: Joel Hedlund | last post by:
Hi! The question of type checking/enforcing has bothered me for a while, and since this newsgroup has a wealth of competence subscribed to it, I figured this would be a great way of learning...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...

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.