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

  • SEARCH
  • Home
  • 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 4553658
In Process

The Archive Base Latest Questions

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

Here is a program that compiles without warning on e.g. GNU C++: $ g++

  • 0

Here is a program that compiles without warning on e.g. GNU C++:

$ g++ -o t -Wall -pedantic -Wshadow t.cpp

$ ./t.exe
Calling barney::barney()
Calling foo::operator()()
Calling barney::barney()

But it completely fails to compile on MSVC++:

$ cl /EHsc t.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.30729.01 for 80x86
Copyright (C) Microsoft Corporation.  All rights reserved.

t.cpp
t.cpp(17) : error C2380: type(s) preceding 'fred' (constructor with return type, or illegal redefinition of current class-name?)
t.cpp(17) : error C2208: 'fred' : no members defined using this type

What’s more, when it does compile, the output is not what I’d expect. Can someone shed some light on what would be the required standard behaviour for this code?

Here it is:

#include <iostream>

using ::std::cerr;

struct fred;

struct foo {
   inline fred operator ()();
};

struct barney {
   barney() : v_(0) { cerr << "Calling barney::barney()\n"; }
   int v_;
};

struct fred : public barney {
   foo fred;
   int joe;
   struct fred memfunc() { return fred(); }
};

inline fred foo::operator ()()
{
   cerr << "Calling foo::operator()()\n"; return fred();
}

int main(int argc, const char *argv[])
{
   fred f;
   f.memfunc();
   return 0;
}

It outputs this:

Calling barney::barney()
Calling foo::operator()()
Calling barney::barney()

But I would expect this:

Calling barney::barney()
Calling barney::barney()

Why do I get the output I do? Is this standard behavior? If it is, why, which sections of the standard are relevant?

In addition to the accepted answer, David Rodriguez gave an excellent answer detailing where it says in the standard that I’m allowed to declare the member named fred of struct fred.

  • 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-21T16:58:41+00:00Added an answer on May 21, 2026 at 4:58 pm

    Because in the fred structure you have a member fred (of type foo) which shadows the definition of struct fred. When you then do:

    return fred();
    

    … the fred refers to the object of type foo rather than the fred struct type, and so the foo () operator is called.

    Notice that the name “fred” refers to two different things – the member, of type foo, and the fred struct type. The compiler must choose one or the other, and it does so according to the rules defined in section 3.4 (“Name Lookup”) of the C++ standard.

    You can force fred to refer to the type using a namespace qualification:

    return ::fred();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

first question here. I'm developing a program in C# (.NET 3.5) that displays files
Here's a short test program: sub foo($;@) { my $sql = shift; my @params
Just wondering what little scripts/programs people here have written that helps one with his
I have a program here: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/ipc.h> #include
I've got a Schroedinger's Cat type of problem here -- my program (actually the
Okay here's the program I have typed up(stdio.h is included also): /* function main
Here is a quick test program: public static void main( String[] args ) {
Here is a little test program: #include <iostream> class Test { public: static void
Here's the purpose of my console program: Make a web request > Save results
Newbie here...can I write one program which incorporates .NET LINQ and also various Java

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.