473,289 Members | 1,791 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,289 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 2054
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: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.