Hello.
Based on this simple test program:
class Test {};
int main()
{
Test t;
const Test u;
}
Why does the definition of 'u' fail? What if all that's wanted is to
have a default constructed Test object? Must one explicitly write so? E.g.:
const Test u = Test();
What's the rationale for such restriction? And, lastly, why does
defining a default constructor solve the issue? I.e.:
class Test
{
public:
Test() {}
};
int main()
{
Test t;
const Test u;
}
Now the compiler accepts it just fine. Why? Pardon me if the answer to
these questions should be obvious. I don't presently find them to be so.
Thank you,
--
Ney André de Mello Zunino 5 9105
which compiler you are using..?
I tried it on VC++ compiler it is not giving any error in both the
cases
since constructor is called during object creation, it doesn't matter
wether the object is const or not
by declaring a object as const the only restrictions that you have on
it is
-- you can only call const methods of that object( ex: methods like
void print() const )
-- you cannot modify any member variables
Ney André de Mello Zunino wrote:
[snip] What's the rationale for such restriction? And, lastly, why does defining a default constructor solve the issue? I.e.:
The code you posted is not your real code.
The code you posted should compile fine without a problem,
since if you don't define any constructor on your own, the compiler
will create a default constructor for you.
So please post your real problem code.
--
Karl Heinz Buchegger kb******@gascad .at
Ney André de Mello Zunino wrote: class Test {};
int main() { Test*t; const*Test*u; }
Why does the definition of 'u' fail?
[8.5]
9. If no initializer is specified for an object, and the object is of
(possibly cv-qualified) non-POD class type (or array thereof), the object
shall be default-initialized; if the object is of const-qualified type,
the underlying class type shall have a user-declared default constructor.
Otherwise, if no initializer is specified for an object, the object and
its subobjects, if any, have an indeterminate initial value 90) ; if the
object or any of its subobjects are of const-qualified type, the program
is ill-formed.
Your object is of const POD type, then the last sentence of the paragraph
applies.
MM
* Karl Heinz Buchegger: Ney André de Mello Zunino wrote: [snip] What's the rationale for such restriction? And, lastly, why does defining a default constructor solve the issue? I.e.:
The code you posted is not your real code. The code you posted should compile fine without a problem, since if you don't define any constructor on your own, the compiler will create a default constructor for you.
So please post your real problem code.
Sorry.
Check out the standard's text quoted by "Max M." in this thread;
some explicit definition of the value is necessary for a const
object.
Unfortunately Visual C++ erronously compiles the code, at least as
I remember it it does.
--
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?
"Alf P. Steinbach" wrote: * Karl Heinz Buchegger: Ney André de Mello Zunino wrote: [snip] What's the rationale for such restriction? And, lastly, why does defining a default constructor solve the issue? I.e.:
The code you posted is not your real code. The code you posted should compile fine without a problem, since if you don't define any constructor on your own, the compiler will create a default constructor for you.
So please post your real problem code.
Sorry.
Check out the standard's text quoted by "Max M." in this thread; some explicit definition of the value is necessary for a const object.
I saw it.
I didn't know that.
I learned.
--
Karl Heinz Buchegger kb******@gascad .at This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: CoolPint |
last post by:
It seems to me that I cannot assign objects of a class which has a constant data
member since the data member cannot be changed once the constructor calls are
completed. Is this the way it is meant to be? Am I not suppose not to have any
constant data member if I am going to have the assignment operator working for
the class?
Or am I...
|
by: nan.li.g |
last post by:
I have this simple code below. When I compiled it, I got the following
error. But after I removed the comment marker(//), i.e. explicitly
defined a constructor, it becomes OK.
The compiler should generate a default constructor for me and the
default one shoule be no different than the one I specified. Why do I
have to specify one in this...
|
by: Marcin Kalicinski |
last post by:
int a; // #1 uninitialized value
int a(0) // #2 zero-initialized value
std::vector<int> v(10); // #3 zero-initialized values (why?)
std::vector<int> v(10, 0); // #4 zero-initialized values
How do I construct a vector of ints without initializing them to zero? Why
is #3 zero-initializing the ints? This is unintutitive,...
|
by: Tony Johansson |
last post by:
Hello Experts!
What does this mean actually.
If you have a template with a type and non-type template argument, say, like
this
template<typename T, int a>
class Array {. . .};
then A<int, 1> and A<int, 2> are different types. Now, if the A template
|
by: rswanster |
last post by:
When I compile and run the following:
#include <iostream>
int main() {
bool f;
std::cout << f << std::endl;
f = not(f);
std::cout << f << std::endl;
}
| |
by: Ben Finney |
last post by:
Howdy all,
I've recently packaged 'enum' in PyPI. In its description, I make the
claim that it creates "immutable" enumeration objects, and that the
enumeration values are "constant" values.
This raises questions.
Is there any difference between a Python immutable value, and a
constant? I suppose "constant" also implies that the *name*...
|
by: tsaar2003 |
last post by:
Hi Pythonians,
To begin with I'd like to apologize that I am not very experienced
Python programmer so please forgive me if the following text does not
make any sense.
I have been missing constants in Python language. There are some
workarounds available, for example the const-module. To me, this looks
quite cumbersome and very...
|
by: Paul Edwards |
last post by:
On ecgs (gcc) 2.91.57 and on gcc 3.2.3 I am getting the
error "initializer element is not constant" on the below
code. The code compiles fine with other compilers I
have. Is this valid C89 code?
extern int *b;
int *a = b;
int main(void)
|
by: Amit Bhatia |
last post by:
Hi,
I am not sure if this belongs to this group. Anyway, my question is as
follows: I have a list (STL list) whose elements are pairs of integers (STL
pairs, say objects of class T). When I create a new object of class T, I
would like to check if this object already exists in the list: meaning one
having same integers. This can be done in...
|
by: pillbug |
last post by:
Hello, I would like to know what the standard says regarding
writing beyond the end of file. For example, in the following
program, is the result always the same or is it system dependent?
#include <cstdio>
int main (int argc, char** argv)
{
char ch = 'A';
FILE* fh = NULL;
|
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: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
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: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
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...
| |