473,463 Members | 1,527 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Legal or not?

Hi!

I tried to compile the following code with Comeau, but it gave me a
puzzling error(The code makes no sense, it is just to show what puzzles me)

class Base
{
public:
Base();
Base(int);
};

class Derived : public Base
{
public:
Derived(int i)
{
Base(0); // Compiles fine, but

Base::Base(0); // Does not compile, Comeau says:

// error: a constructor or destructor may not have its
// address taken

}
};

Is the line valid or not?

greets,

Christoph

Jul 19 '05 #1
16 1442
"Christoph Rabel" <od**@hal9000.vc-graz.ac.at> wrote in message
news:3f***********************@aconews.univie.ac.a t...
Hi!

I tried to compile the following code with Comeau, but it gave me a
puzzling error(The code makes no sense, it is just to show what puzzles me)
class Base
{
public:
Base();
Base(int);
};

class Derived : public Base
{
public:
Derived(int i)
{
Base(0); // Compiles fine, but
Creates a temporary object of type 'Base'.
Base::Base(0); // Does not compile, Comeau says:
I am not sure why it said you should not take the address, because I
would have thought the line above would mean a call to the c'tor, which
cannot work.
// error: a constructor or destructor may not have its
// address taken

}
};

Is the line valid or not?


Even though I do not quite understand why Comeau prints /that/ error
message, I still think it is invalid.

hth
--
jb

(replace y with x if you want to reply by e-mail)
Jul 19 '05 #2

"Christoph Rabel" <od**@hal9000.vc-graz.ac.at> wrote in message news:3f***********************@aconews.univie.ac.a t...
Base(0); // Compiles fine, but
This probably does not do what you think. This creates a temporary object of
type Base (initialized with a 0). It is not, as you might be expecting, a call
to the Base class constructor on the curent object.
Base::Base(0); // Does not compile, Comeau says:

This line isn't valid. Base::Base is not valid at all in this context.
Comeau's error message is a bit obtuse but since it's not a valid
construct it's just trying to take a stab at what it thinks you're
trying to do.

What you really want is:

Derived(int i) : Base(0) { }
Jul 19 '05 #3

"Jakob Bieling" <ne*****@gmy.net> wrote in message news:bj*************@news.t-online.com...
> Base::Base(0); // Does not compile, Comeau says:


I am not sure why it said you should not take the address, because I
would have thought the line above would mean a call to the c'tor, which
cannot work.


Can't mean that, because constructors don't have names. Base::Base
just isn't a valid sequence here.
Jul 19 '05 #4
Ron Natalie wrote:
"Christoph Rabel" <od**@hal9000.vc-graz.ac.at> wrote in message news:3f***********************@aconews.univie.ac.a t...
Base(0); // Compiles fine, but


This probably does not do what you think. This creates a temporary object of
type Base (initialized with a 0). It is not, as you might be expecting, a call
to the Base class constructor on the curent object.


Oh, I know that. I simply was confused that I am allowed to write
Base(0); but not to write Base::Base(0);
Base::Base(0); // Does not compile, Comeau says:

This line isn't valid. Base::Base is not valid at all in this context.
Comeau's error message is a bit obtuse but since it's not a valid
construct it's just trying to take a stab at what it thinks you're
trying to do.


I'd like to understand why the first line is valid and the second not. I
thought, that they are essentially "equal".

Is it because of 12.1.2, Constructors have no name and are never found
during name lookup?

thx,

Christoph

Jul 19 '05 #5
"Ron Natalie" <ro*@sensor.com> wrote in message
news:3f***********************@news.newshosting.co m...

"Jakob Bieling" <ne*****@gmy.net> wrote in message

news:bj*************@news.t-online.com...
> Base::Base(0); // Does not compile, Comeau says:


I am not sure why it said you should not take the address, because I
would have thought the line above would mean a call to the c'tor, which
cannot work.


Can't mean that, because constructors don't have names. Base::Base
just isn't a valid sequence here.

I know, but most people who write that think they can call c'tors
explicitly, which is also what it looks like: a call to that c'tor
(regardsless of the fact that this is illegal). So I would have expected the
compiler to print out something along that line.

regards
--
jb

(replace y with x if you want to reply by e-mail)
Jul 19 '05 #6
"Christoph Rabel" <od**@hal9000.vc-graz.ac.at> wrote in message
news:3f***********************@aconews.univie.ac.a t...
Ron Natalie wrote:
"Christoph Rabel" <od**@hal9000.vc-graz.ac.at> wrote in message news:3f***********************@aconews.univie.ac.a t...
Base(0); // Compiles fine, but


This probably does not do what you think. This creates a temporary object of
type Base (initialized with a 0). It is not, as you might be expecting, a call to the Base class constructor on the curent object.


Oh, I know that. I simply was confused that I am allowed to write
Base(0); but not to write Base::Base(0);
Base::Base(0); // Does not compile, Comeau says:

This line isn't valid. Base::Base is not valid at all in this context.
Comeau's error message is a bit obtuse but since it's not a valid
construct it's just trying to take a stab at what it thinks you're
trying to do.


I'd like to understand why the first line is valid and the second not. I
thought, that they are essentially "equal".

Suppose you had a typedef 'test' for 'int' inside 'Base'. Then you could
do this: Base::test (0). Now you did Base::Base (0), so you are trying to
find something inside the 'Base' namespace. But there is nothing you could
find like that; the 'Base' class is not inside the 'Base' namespace. This is
why the two lines are not equal at all.

hth
--
jb

(replace y with x if you want to reply by e-mail)
Jul 19 '05 #7

"Christoph Rabel" <od**@hal9000.vc-graz.ac.at> wrote in message news:3f575885$0$25930
Oh, I know that. I simply was confused that I am allowed to write
Base(0); but not to write Base::Base(0);
Base is a type name in this context, it's legal. Base::Base is not.
I'd like to understand why the first line is valid and the second not. I
thought, that they are essentially "equal".
Is it because of 12.1.2, Constructors have no name and are never found
during name lookup?


Paritally. The constructor doesn't have a name. Base::Base doesn't
refer to it, Base doesn't refer to it. Base works becuase it is a type
name. Base::Base is nonsense, there is no legitimate qualified identifier
of that name.
Jul 19 '05 #8

"Christoph Rabel" <od**@hal9000.vc-graz.ac.at> wrote in message
news:3f***********************@aconews.univie.ac.a t...
Hi!

I tried to compile the following code with Comeau, but it gave me a
puzzling error(The code makes no sense, it is just to show what puzzles me)
class Base
{
public:
Base();
Base(int);
};

class Derived : public Base
{
public:
Derived(int i)
{
Base(0); // Compiles fine, but

Base::Base(0); // Does not compile, Comeau says:

// error: a constructor or destructor may not have its
// address taken

}
};

Is the line valid or not?


I don't understand the error message, but it's just the compiler struggling
to make sense out of what you really wanted to do (as I am :-) I'm sure
Base(0); isn't what you want to do either. You need to read up on
"initializer list". You need to "call" the base constructor in a special -
NOT the way you attempted.
Jul 19 '05 #9

"Christoph Rabel" <od**@hal9000.vc-graz.ac.at> wrote in message
news:3f***********************@aconews.univie.ac.a t...
Ron Natalie wrote:
"Christoph Rabel" <od**@hal9000.vc-graz.ac.at> wrote in message news:3f***********************@aconews.univie.ac.a t...
Base(0); // Compiles fine, but
This probably does not do what you think. This creates a temporary object of type Base (initialized with a 0). It is not, as you might be expecting, a call to the Base class constructor on the curent object.


Oh, I know that. I simply was confused that I am allowed to write
Base(0); but not to write Base::Base(0);


Because there is no "Base" defined inside the "Base".
I'd like to understand why the first line is valid and the second not. I
thought, that they are essentially "equal".

Is it because of 12.1.2, Constructors have no name and are never found
during name lookup?


No, it's because Base is in the global scope, and nowhere else. Under what
conditions would you normally use the :: operator?
Jul 19 '05 #10

"Jakob Bieling" <ne*****@gmy.net> wrote in message
news:bj*************@news.t-online.com...
I know, but most people who write that think they can call c'tors
explicitly, which is also what it looks like: a call to that c'tor
(regardsless of the fact that this is illegal). So I would have expected the compiler to print out something along that line.


I prefer to think of it that you can call a constructor explicitly in the
initializer list. From the user's point of view (if not the compiler's),
it's indistinguishable from any other function call (someone correct me if
I'm wrong.)
Jul 19 '05 #11

"jeffc" <no****@nowhere.com> wrote in message news:3f********@news1.prserv.net...

"Jakob Bieling" <ne*****@gmy.net> wrote in message
news:bj*************@news.t-online.com...
I know, but most people who write that think they can call c'tors
explicitly, which is also what it looks like: a call to that c'tor
(regardsless of the fact that this is illegal). So I would have expected the
compiler to print out something along that line.


I prefer to think of it that you can call a constructor explicitly in the
initializer list.


You may think of it that way, but it is wrong. It is NOT calling the constructor
there and thinking that way just adds to the confusion. All you are doing is providing
initializers for the objects. Specifically, if you think that you are calling there, you might
think that the order that you specify the initializers have some meaning. Further you
can specify initializers in the list for things that don't even have constructors.
From the user's point of view (if not the compiler's),
it's indistinguishable from any other function call (someone correct me if
I'm wrong.)


I contend you are wrong. It's nothing like a function call. It's more like remembering
what the initializers are so when it's time to invoke the constructor we know what they
are.
Jul 19 '05 #12
Jakob Bieling wrote:
"Christoph Rabel" <od**@hal9000.vc-graz.ac.at> wrote in message
news:3f***********************@aconews.univie.ac.a t...

Suppose you had a typedef 'test' for 'int' inside 'Base'. Then you could
do this: Base::test (0). Now you did Base::Base (0), so you are trying to
find something inside the 'Base' namespace. But there is nothing you could
find like that; the 'Base' class is not inside the 'Base' namespace. This is
why the two lines are not equal at all.


Doesnt this contradict 9.2:

"A classname is inserted into the scope in which it is
declared immediately after the classname is seen.
The classname is also inserted into the scope of the class
itself."

Or am I misunderstanding something here?

Christoph

Jul 19 '05 #13

"Ron Natalie" <ro*@sensor.com> wrote in message
news:3f***********************@news.newshosting.co m...

I prefer to think of it that you can call a constructor explicitly in the initializer list.
You may think of it that way, but it is wrong. It is NOT calling the

constructor there and thinking that way just adds to the confusion.
On the contrary, it makes initializer lists more clear to most people.
Specifically, if you think that you are calling there, you might
think that the order that you specify the initializers have some meaning.
One might, but I wouldn't. There aren't any semicolons there, so I wouldn't
think that. That's beside my point.
Further you can specify initializers in the list for things that don't even have constructors.

Again, irrelevant and beside my point. Those things are a syntactical
convenience (just like int i(1);) and couldn't possibly be function calls.
From the user's point of view (if not the compiler's),
it's indistinguishable from any other function call (someone correct me if I'm wrong.)


I contend you are wrong. It's nothing like a function call. It's more

like remembering what the initializers are so when it's time to invoke the constructor we know what they are.


"It's nothing like a function call." You can't back that one up. It's very
much like a function call. Run it through your debugger sometime and
explain to me why it doesn't act like a function call from the user's point
of view.
Jul 19 '05 #14

"Ron Natalie" <ro*@sensor.com> wrote in message
news:3f***********************@news.newshosting.co m...
> I prefer to think of it that you can call a constructor explicitly in the initializer list.


You may think of it that way, but it is wrong. It is NOT calling the

constructor there and thinking that way just adds to the confusion. All you are doing is providing initializers for the objects. Specifically, if you think that you are calling there, you might think that the order that you specify the initializers have some meaning.


As further evidence that this is really a red herring, consider the
following:
int a(){return 1;}
int b(){return 2;}
int c(){return 3;}

int x = a() + b() * c();
compare with
int x = c() * b() + a();
compare with
int x = a() * b() + c();

According to your logic, you should not think of these as function calls
because then you might think that the order you specify the names has some
meaning. In both this case and the initializer list case, functions are
being called (constructors *are* special functions), and the order in which
they're called depends on something other than the sequential order in which
they appear on the page.
Jul 19 '05 #15
"Christoph Rabel" <od**@hal9000.vc-graz.ac.at> wrote in message
news:3f***********************@aconews.univie.ac.a t...
Jakob Bieling wrote:
"Christoph Rabel" <od**@hal9000.vc-graz.ac.at> wrote in message
news:3f***********************@aconews.univie.ac.a t...

Suppose you had a typedef 'test' for 'int' inside 'Base'. Then you could do this: Base::test (0). Now you did Base::Base (0), so you are trying to find something inside the 'Base' namespace. But there is nothing you could find like that; the 'Base' class is not inside the 'Base' namespace. This is why the two lines are not equal at all.


Doesnt this contradict 9.2:

"A classname is inserted into the scope in which it is
declared immediately after the classname is seen.
The classname is also inserted into the scope of the class
itself."

Or am I misunderstanding something here?

I do not think you misunderstood, since this does sound pretty much like
your two lines should be identical. I did not know that rule before. Now you
also made me curious as to why it does not work *g*

regards
--
jb

(replace y with x if you want to reply by e-mail)
Jul 19 '05 #16
Christoph Rabel wrote in
news:3f***********************@aconews.univie.ac.a t:
Jakob Bieling wrote:
"Christoph Rabel" <od**@hal9000.vc-graz.ac.at> wrote in message
news:3f***********************@aconews.univie.ac.a t...

Suppose you had a typedef 'test' for 'int' inside 'Base'. Then
you could
do this: Base::test (0). Now you did Base::Base (0), so you are
trying to find something inside the 'Base' namespace. But there is
nothing you could find like that; the 'Base' class is not inside the
'Base' namespace. This is why the two lines are not equal at all.


Doesnt this contradict 9.2:

"A classname is inserted into the scope in which it is
declared immediately after the classname is seen.
The classname is also inserted into the scope of the class
itself."

Or am I misunderstanding something here?


It maybe that "inserted into the scope" dosen't mean becomes
a member. AFAIKT the above wording (the 2nd part) is so the
class-name is in scope in the class declaration even though
it hasn't been fully declared yet.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #17

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

Similar topics

1
by: pakkocool | last post by:
¿Como ganar dinero en internet? Lee atentamente el siguiente texto, es super interesante y te hara ganar muchos dolares si sigues las instrucciones y le pones empeño: Hace unos días que...
3
by: Chris Johnson | last post by:
Greetings all: I come across an interesting question (to me anyway) and I do not know the answer. Code/Questions follow: #include <iostream> #if 0 // uncommenting *should* make call...
7
by: Nice | last post by:
Hi. I read somewhere that in XML the tag <xml> is not legal. But in W3C specs I can't find this statement. Does anyone know about it ?
0
by: mykidisgollum | last post by:
Greetings, I have code which prints a document who's attributes are saved as flags in a database. One of the those attributes is letter or legal. When I am printing, I use the following...
7
by: __PPS__ | last post by:
Actually what I mean is that - if I have some memory buffer, lets say char a; and then I do like this: DWORD num = 0x1234; *(DWORD*)a = num; (1) *(DWORD*)(a+1) = num; (2) either...
6
by: aj | last post by:
I currently have 2 official DB2 Workgroup Edition licenses for my 2 v8 production servers. I also have tech support/software upgrade agreements in place for both servers. I am interested in...
2
by: Thomas Paul Diffenbach | last post by:
I'm trying to write a space efficient string (nul terminated array of char), storing the characters directly unless the number of characters is too large to be so stored, and storing a pointer to...
1
by: Wiktor Zychla | last post by:
is it legal to distribute axshdocvw, shdocvw and mshtml.dll with my application? is it legal to distribute Microsoft.mshtml.dll from 'Primary Interop Assemblies' folder (I assume it comes with...
11
by: Alberto Giménez | last post by:
Hi, I've seen some object oriented programming bits out there and i'm not sure if they're legal. For example: struct Object { int field1; int field2; }; struct SubObject { int field1; /*...
2
by: Army1987 | last post by:
Is this program legal C89? /* no headers included */ int main(void) { if (sizeof (exit(0), 0), ((void (* )(int))&exit)( (puts((const char *)"hello, world"), 0) ), 0) {
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:
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,...
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
agi2029
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,...
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...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.