object construction recognized as function declaration

T

tthunder

Hi @all,

there are always details, why it isn't possible to create a consistent
framework :(
However, here some code:

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

class foo
{
public:
foo(){}
};

class fooConst
{
public:
explicit fooConst(const foo &p_Foo){}
void doSomething() {}
};

void test()
{
fooConst myObject(foo());
myObject.doSomething(); // <--- Error
}

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

This doen't work on all compilers I have tested, because "fooConst
myObject(foo());" is assumed to be a funtion declaration!

I know that I could write:

foo aFoo;
fooConst myObject(aFoo);

But I don't want to!

Is there another keyword, possibility,........ you can think of ???

Greetings,
Kirsten
 
A

Alf P. Steinbach

* (e-mail address removed):
Hi @all,

there are always details, why it isn't possible to create a consistent
framework :(
However, here some code:

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

class foo
{
public:
foo(){}
};

class fooConst
{
public:
explicit fooConst(const foo &p_Foo){}
void doSomething() {}
};

void test()
{
fooConst myObject(foo());
myObject.doSomething(); // <--- Error
}

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

This doen't work on all compilers I have tested, because "fooConst
myObject(foo());" is assumed to be a funtion declaration!

I know that I could write:

foo aFoo;
fooConst myObject(aFoo);

But I don't want to!

Is there another keyword, possibility,........ you can think of ???

You can

* Declare the argument object as a local variable, or

* use extra parentheses, or

* use the old "=" initialization syntax.

You should

* Try to include fewer misdirections in your examples.

Hth.
 
A

Alf P. Steinbach

* (e-mail address removed):
As I already mentioned, this would be a nasty break in constistence. I
don't really want to.

It may be for the best, for it wouldn't suprise me if otherwise you'll
end up with a dangling reference or pointer.

Where exactly?

You can always parenthesize an expression, such as an actual argument,
and if you look this up in the standard you'll find just that as an
example on how to do this.

Not possible, because explicit

I'm sorry, but that's incorrect. In some news-groups a good way to get
a concrete example is to say "that's impossible". In this group, you're
more likely to elicit responses such as "what's your textbook?" (this
one is common because there really are a lot of bad C++ textbooks).

Anyway, I'll let you figure out why your statement is incorrect.

Just ask again if that's, uh, "impossible"... ;-)
 
T

tthunder

fooConst myObject( ( foo() ) );
^ ^

Sorry, this does not work on my compiler Borland C++ Builder 6.0...
I've already tried that.

fooConst myObject = myObject(foo());

After I have posted my last reply I thought about this, right... but
I'm sorry, because I have forgotten an important fact in my example:
the assignment operator and copy constructor are hidden!
 
R

red floyd

Hi @all,

there are always details, why it isn't possible to create a consistent
framework :(
However, here some code:

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

class foo
{
public:
foo(){}
};

class fooConst
{
public:
explicit fooConst(const foo &p_Foo){}
void doSomething() {}
};

void test()
{
fooConst myObject(foo());
myObject.doSomething(); // <--- Error
}

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

This doen't work on all compilers I have tested, because "fooConst
myObject(foo());" is assumed to be a funtion declaration!

I know that I could write:

foo aFoo;
fooConst myObject(aFoo);

google for "most vexing parse"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top