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? 10 2076
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.
* 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?
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?
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.
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.
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
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.
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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 parent class, and I
can't make some "helper" class a friend of the parent class to help in
accessing the data.)
Problem: I want to derive a...
|
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? Which of these fundamental language support
features could I expect to be absent (and anything else I might have
missed):
static data zeroing...
|
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
|
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: VarArray::funct were
an extern, but it is declared in the same file (q.v.). What is the
remedy for this?
=================
|
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 (also copied below for easier reference):
http://groups.google.it/groups?hl=en&lr=&safe=off&selm=6beiuk%24cje%40netlab.cs.rpi.edu&rnum=95
I don't...
| |
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 type of its own class. Non-static data members can
not be declared as type of its own class (excluding pointers and
references).
|
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 organised under a network topology
('NetworkTopology' class).
The problem is that the properties (data members) of network elements
depend on...
|
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 DataSet. The code I am using is as
follows:
Private Sub btnDownloadDB_Click(ByVal sender As System.Object, ByVal e As...
|
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, attributes, etc.)
as parameters to the class's member functions or would you normally have
parameters that are not data members of the class? Or is...
|
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 from the experts. I feel
there's a tradeoff between clear, easily readdable and extensible code
on one side, and safe code providing early errors and...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |