Question

I expected to initialize object of B here, but instead I got function declaration:

#include <iostream>
using namespace std;

class A {};

class B { 
    public: 
        B(const A&) { 
        cout << "B: conversion constructor\n"; 
    } 
};

int main()
{
    B b( A() ); //function declaration: B b( A(*)() );
    b.test();
}

The output is: request for member 'test' in 'b', which is of non-class type 'B( A(*)() )'*

Why isn't the constructor invoked in this situation?

Was it helpful?

Solution

This is one of the many cases where the parser thinks that a variable declaration is a function declaration, try writing it as:

B b = A() ;  // Now the compiler doesn't think that it's a function declaration
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top