Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6587877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:58:31+00:00 2026-05-25T16:58:31+00:00

Assume we have a class with no default constructor: class Foo { public: Foo(int

  • 0

Assume we have a class with no default constructor:

class Foo {
 public:
  Foo(int data) : _data(data) { }
  int data(void) const { return _data; }
 private:
  int _data;
};

Why does this compile and what does it do:

Foo x();

Even though the above compiles, you can’t do any of the following:

x.data();   // doesn't compile: request for member 'data' in 'x', which is of non-class type 'Foo()'
x->data();  // doesn't compile: request for member 'data' in 'x', which is of non-class type 'Foo()'
x().data(); // compiles!!! but linking fails with: undefined reference to `x()' 
x()->data();// doesn't compile:  base operand of '->' has non-pointer type 'Foo'

I guess I’m just confused about what adding the () after the x does, and why the language allows this? Is this every allowable and/or useful? It seems that no Foo instance is allocated on the stack either, because even with -Wunused-variable, no warning occurs about the x in Foo x();

In contrast, both of the following do not compile:

Foo *x = new Foo;  //  error: no matching function for call to 'Foo::Foo()
Foo *y = new Foo();//  error: no matching function for call to 'Foo::Foo()

This seems more consistent, I don’t understand what is going on with Foo x();

EDIT: Ok, figured it out. Foo x(); is a prototype for a function ‘x’, which takes no parameters and returns a Foo. Since the function isn’t defined anywhere, trying to use it like a Foo instance (x.data()) or Foo pointer (x->data()) don’t work. x()->data() doesn’t work because x() is of type Foo, not pointer to Foo. x().data() compiles because x() returns Foo, which has a data method, but it fails to link because the function x has not been defined. Whew.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T16:58:31+00:00Added an answer on May 25, 2026 at 4:58 pm
    Foo x();
    

    This doesn’t create object x of type Foo. Instead you are declaring a function x whose return type is Foo.

    This C++ FAQ entry should be helpful.

    Now,

    Foo *x = new Foo;  //  error: no matching function for call to 'Foo::Foo()
    

    new Foo invokes the default constructor and you don’t have one and is the reason for the error. If a class provides constructor then the default constructor is not provided by the compiler.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have business class: public class Foo { public int Prop1 {get;set;}
Assume I have a class foo, and wish to use a std::map to store
Assume I have a class that looks like this: class Sample { public string
Assume I have a class class A { char *attr1,*attr2; public: . . .
Assume I have a C# class like this: [XmlRoot(floors)] public class FloorCollection { [XmlElement(floor)]
Assume the following: we have class B, which is a private class nested inside
So I have a class with a single string parameter in its constructor: public
I have the following Model class: package com.swaranga.model; public final class Book { private
Let us assume I have two classes: class Base{}; class Derived: public Base{}; none
Lets assume we have a class car. How would You name parameters of function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.