Bug 26024 - Initializing using methods of class object passed to constructor
Summary: Initializing using methods of class object passed to constructor
Status: RESOLVED DUPLICATE of bug 9217
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.4.4
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-01-30 05:14 UTC by Don@Skyler.com
Modified: 2006-01-30 12:59 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Don@Skyler.com 2006-01-30 05:14:06 UTC
#include	<iostream>

struct	B1
{
	B1(int i):i_(i)	{	}
	int i_;
};

struct	B2
{
	B2	(void)	{	}
};

struct	Initializer
{
	B1	b1(void)	const	{	return	B1(1);	}
	B2	b2(void)	const	{	return	B2();		}
};

struct	D	:	public	B1,	public	B2
{
	D	(	const Initializer	&init	)	:	B1(init.b1()),	B2(init.b2())	{	}
};

int	main	(void)
{
	d(Initializer());

//	The line above produces the following error in g++ 3.4.4 under cygwin:

//	bug.cpp: In function `int main()':
//	bug.cpp:35: error: request for member `i_' in `d', which is of non-class type `D
//	()(Initializer (*)())'

//	Commenting out that line and uncommenting the following two lines:

//		Initializer	init;
//		D	d(init);

//	compiles without error in g++ 3.4.4 under cygwin, but produces the output:
//	i_=0

//	Under MS Visual C++ 6.0 either one compiles without error and produces:
//	i_=1
//	which appears to be correct.

	std::cout	<<	"i_="	<<	d.i_	<<	std::endl;

	return	0;
}
Comment 1 Don@Skyler.com 2006-01-30 05:30:05 UTC
Somehow when I copied and pasted the first line in main came out wrong; it should be:

	D	d(Initializer());

which produces the apparently incorrect error as noted.
Comment 2 Richard Biener 2006-01-30 10:29:01 UTC
D       d(Initializer());

parses as a function declaration, as you see from the error (which is on
the cout line btw.):

//      bug.cpp: In function `int main()':
//      bug.cpp:35: error: request for member `i_' in `d', which is of
non-class type `D
//      ()(Initializer (*)())'

use

D      d((Initializer()));
Comment 3 Don@Skyler.com 2006-01-30 12:39:21 UTC
Subject: Re:  Initializing using methods of class object passed to constructor

On 30 Jan 2006 10:29:01 -0000, you wrote:

>
>
>------- Comment #2 from rguenth at gcc dot gnu dot org  2006-01-30 10:29 -------
>D       d(Initializer());
>
>parses as a function declaration, as you see from the error (which is on
>the cout line btw.):

Sorry, I don't get it.  If the line were something like:

D d(Initializer init);

or 

D d(Initializer);

I could understand.  But

D d(Initializer())

doesn't look like a function declaration to me.  Initializer() is a
(constructor) function call.  It's not the right form for a parameter
in a function prototype.

Also, this was only one of two problems: when you replace that line
with:

Initializer init;
D d(init);

you get the wrong output.  It should output i_=1.  I was getting i_=0
when I posted the bug.  At one point later I got i_=97.  Seems like
it's leaving the i_ member undefined.

>//      bug.cpp: In function `int main()':
>//      bug.cpp:35: error: request for member `i_' in `d', which is of
>non-class type `D
>//      ()(Initializer (*)())'
>
>use
>
>D      d((Initializer()));
Comment 4 Andrew Pinski 2006-01-30 12:59:33 UTC
Reopening to ...
Comment 5 Andrew Pinski 2006-01-30 12:59:51 UTC
Close as a dup of bug 9217.

*** This bug has been marked as a duplicate of 9217 ***