object construction recognized as function declaration

Discussion in 'C++' started by tthunder, Jun 2, 2006.

  1. tthunder

    tthunder Guest

    Hi @all,

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


    class foo

    class fooConst
    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 ???

    tthunder, Jun 2, 2006
    1. Advertisements

  2. * :
    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.

    Alf P. Steinbach, Jun 2, 2006
    1. Advertisements

  3. tthunder

    tthunder Guest

    You can
    As I already mentioned, this would be a nasty break in constistence. I
    don't really want to.
    Where exactly?
    Not possible, because explicit

    tthunder, Jun 2, 2006
  4. tthunder

    Alan Johnson Guest

    fooConst myObject( ( foo() ) );
    ^ ^
    fooConst myObject = myObject(foo());
    Alan Johnson, Jun 2, 2006
  5. tthunder

    Alan Johnson Guest


    fooConst myObject = fooConst(foo());
    Alan Johnson, Jun 2, 2006
  6. * :
    It may be for the best, for it wouldn't suprise me if otherwise you'll
    end up with a dangling reference or pointer.

    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.

    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"... ;-)
    Alf P. Steinbach, Jun 2, 2006
  7. tthunder

    tthunder Guest

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

    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!
    tthunder, Jun 2, 2006
  8. tthunder

    red floyd Guest

    google for "most vexing parse"
    red floyd, Jun 2, 2006
  9. tthunder

    tthunder Guest

    Thank you for this nice headword!
    tthunder, Jun 2, 2006
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.