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

A question on variable declarations/initializations

Hi,

consider the following program:

// --------------
class A
{
public:
A(int i) { }
};

class B
{
public:
B(const A&) { }
void f(int) const { }
};

int main()
{
int i(1);

// Declaring a variable v of type B, initialized with A(i):
B v(A(i)); // Nope, this is not producing the intended behaviour, see
error messages below
// Working alternatives:
// 1) B v((A)(i));
// 2) B v(i);
// 3) B v(1);
// 4) B v = B(A(i));

v.f(i); // Sun Workshop 5.2: B(*)(A) is not a structure type
// GCC 3.3.1: request for member b in b(A), which
// is of non-aggregate type B()(A)

return 0;
}

// --------------

As far as I understand, "B v(A(i));" is interpreted as a "pointer v to a
function taking a parameter of type A and returning B". In that case,
naturally, f cannot be called on v.

I don't understand, however, why "B v(A(i));" shouldn't equally well be seen
as "variable v of type B, initialized with the temporary A(i)". For example,
what's the difference to alternative 1), "B v((A)(i));" ?

Do I miss something trivial? I would appreciate if someone could elaborate
in detail on how this declaration is interpreted.

Many thanks,
Andreas
Jul 22 '05 #1
6 5183
Andreas Wachowski wrote:
Hi,

consider the following program:

// --------------
class A
{
public:
A(int i) { }
};

class B
{
public:
B(const A&) { }
void f(int) const { }
};

int main()
{
int i(1);

// Declaring a variable v of type B, initialized with A(i):
B v(A(i)); // Nope, this is not producing the intended behaviour, see
error messages below
// Working alternatives:
// 1) B v((A)(i));
// 2) B v(i);
// 3) B v(1);
// 4) B v = B(A(i));

v.f(i); // Sun Workshop 5.2: B(*)(A) is not a structure type
// GCC 3.3.1: request for member b in b(A), which
// is of non-aggregate type B()(A)

return 0;
}

// --------------

As far as I understand, "B v(A(i));" is interpreted as a "pointer v to a
function taking a parameter of type A and returning B". In that case,
naturally, f cannot be called on v.

I don't understand, however, why "B v(A(i));" shouldn't equally well be seen
as "variable v of type B, initialized with the temporary A(i)". For example,
what's the difference to alternative 1), "B v((A)(i));" ?

Do I miss something trivial? I would appreciate if someone could elaborate
in detail on how this declaration is interpreted.


For whatever reason, v is being interpeted as a function declaration
returning a B.

error from gcc:
request for member `f' in `v', which is of non-class type `B ()(A)'

Comeau gives a similar error.

The interesting thing is that this also works as you expect:

B v(A(1));

I don't understand how A(i) can be considered a type and not an
expression, or even that A(1) is an expression not when A(i) is not.
Also given that 3 compilers all seem to agree, I am a bit perplexed.
Jul 22 '05 #2
Gianni Mariani wrote:
Andreas Wachowski wrote:
Hi,

consider the following program:

// --------------
class A
{
public:
A(int i) { }
};

class B
{
public:
B(const A&) { }
void f(int) const { }
};

int main()
{
int i(1);

// Declaring a variable v of type B, initialized with A(i):
B v(A(i)); // Nope, this is not producing the intended behaviour,
see
error messages below
// Working alternatives:
// 1) B v((A)(i));
// 2) B v(i);
// 3) B v(1);
// 4) B v = B(A(i));

v.f(i); // Sun Workshop 5.2: B(*)(A) is not a structure type
// GCC 3.3.1: request for member b in b(A), which
// is of non-aggregate type B()(A)

return 0;
}

// --------------

As far as I understand, "B v(A(i));" is interpreted as a "pointer v
to a function taking a parameter of type A and returning B". In that
case, naturally, f cannot be called on v.

I don't understand, however, why "B v(A(i));" shouldn't equally well
be seen as "variable v of type B, initialized with the temporary
A(i)". For example, what's the difference to alternative 1), "B
v((A)(i));" ?

Do I miss something trivial? I would appreciate if someone could
elaborate in detail on how this declaration is interpreted.
For whatever reason, v is being interpeted as a function declaration
returning a B.

error from gcc:
request for member `f' in `v', which is of non-class type `B
()(A)'

Comeau gives a similar error.

The interesting thing is that this also works as you expect:

B v(A(1));

I don't understand how A(i) can be considered a type and not an
expression,


B v(A(i)) declares a funcion named v that takes as a parameter named i
an object of type A and returns an object of type B. It's equivalent
to:

B v(A i)
or even that A(1) is an expression not when A(i) is not.


1 can't be the name of a parameter, but i can.

Jul 22 '05 #3
"Rolf Magnus" <ra******@t-online.de> wrote in message
news:cf*************@news.t-online.com...

[snip]
B v(A(i)) declares a funcion named v that takes as a parameter named i
an object of type A and returns an object of type B. It's equivalent
to:

B v(A i)


So far, so good; thanks for the answer. I am still surprised, though, I
don't think I have seen this before. Can someone point me to a section in
the standard (or some book or other source) explaining this, perhaps? How
can one know (i.e. without relying on the compiler's error message) that the
statement "B v(A(i))" will not declare an element v of type B, initialized
with A(i)?

Your help is appreciated!

Thanks in advance, Andreas
Jul 22 '05 #4

"Andreas Wachowski" <an***************@gmx.de> wrote in message news:2o************@uni-berlin.de...
So far, so good; thanks for the answer. I am still surprised, though, I
don't think I have seen this before. Can someone point me to a section in
the standard (or some book or other source) explaining this, perhaps? How
can one know (i.e. without relying on the compiler's error message) that the
statement "B v(A(i))" will not declare an element v of type B, initialized
with A(i)?

It's amplified in section 8.2 of the standard (Ambiguity Resolution). The rule
that when there is an ambiguity between a declaration and an expression, the
declaration wins out.

The ambiguity is if A(i) is a declaration of the parameter i of type A, or a cast
expression A(i), it is resolved as the declaration.

-Ron

Jul 22 '05 #5

"Ron Natalie" <ro*@sensor.com> wrote in message
news:41**********************@news.newshosting.com ...
It's amplified in section 8.2 of the standard (Ambiguity Resolution).

The rule that when there is an ambiguity between a declaration and an expression, the declaration wins out.

The ambiguity is if A(i) is a declaration of the parameter i of type A, or a cast expression A(i), it is resolved as the declaration.


It never even occured to me this could be ambiguous, because I didn't know
you could declare parameters this way. Am I missing something basic, or
obscure?
Jul 22 '05 #6
jeffc wrote:
The ambiguity is if A(i) is a declaration of the parameter i of type
A, or a cast expression A(i), it is resolved as the declaration.


It never even occured to me this could be ambiguous, because I didn't
know you could declare parameters this way. Am I missing something
basic, or obscure?


You're missing that if you declare something like:

A (i);

the parens are superfluous and ignored.

Jul 22 '05 #7

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

Similar topics

83
by: Alexander Zatvornitskiy | last post by:
Hello All! I'am novice in python, and I find one very bad thing (from my point of view) in language. There is no keyword or syntax to declare variable, like 'var' in Pascal, or special syntax in...
134
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that...
8
by: Jim Moon | last post by:
Hi. I'm curious about this syntax: <variable>=function(){...} I'm not finding a definition of this syntax, but I see it used at this website:...
7
by: Antonio | last post by:
I'm developing the firmware for a slave in a comunication channel. Now there is certain information (namely the addresses of the slave and the master) that must be changeable while the device is up...
3
by: web1110 | last post by:
Is this legit? I thought that class members could not be allocated in the class declaration. I thought this had to go into the constructor. If this is OK, what are the associated rules? class...
37
by: Joergen Bech | last post by:
(Slightly religious question): Suppose I have the following class: ---snip--- Public Class MyClass Private _MyVariable As Integer Public Property MyVariable() As Integer Get
26
by: samjnaa | last post by:
Hello. Please tell me whether this feature request is sane (and not done before) for python so it can be posted to the python-dev mailing list. I should say first that I am not a professional...
2
by: Shraddha | last post by:
Can we declare extern variable as static? What will be the scope of the variable then? What if we change the value of the variable in some other function? Also can someone tell me that if we can...
11
by: Jef Driesen | last post by:
I have the following problem in a C project (but that also needs to compile with a C++ compiler). I'm using a virtual function table, that looks like this in the header file: typedef struct...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.