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 8178905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:52:45+00:00 2026-06-06T23:52:45+00:00

Consider the following code snippet: #include <iostream> using namespace std; struct Element { void

  • 0

Consider the following code snippet:

#include <iostream>

using namespace std;

struct Element {
    void SetVisible(bool) { cout << "Called SetVisible on Element" << endl; }
};

struct ElementQuery {
    ElementQuery(Element * e) : element(e) { }
    Element * Get() const { return element; }
    Element * element;
};

namespace A {

static void SetVisible(ElementQuery const& element, bool show) {
    cout << "Called SetVisible on ElementQuery" << endl;
    SetVisible(element.Get(), show);
}

static void SetVisible(Element * element, bool show) {
    element->SetVisible(show);
}

};

int main() {
    Element * e = new Element();
    ElementQuery q(e);
    A::SetVisible(q, true);
    delete e;
    return 0;
}

When run, program fails because of infinite recursion in call SetVisible(element.GetFirst(), show). As I presume that is so because of function SetVisible(Element * element, bool show) is not declared yet at the time of call though it fits overload resolution better.

But when I change namespace A to struct A, recompile and run, everything works fine. Program prints two lines to cout and ends gracefully.

My question is: why does the second call “sees” second declaration of SetVisible and what are the differences between such declarations?

  • 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-06-06T23:52:46+00:00Added an answer on June 6, 2026 at 11:52 pm

    It’s because the namespace is processed in order and there exists a conversion between Element and ElementQuery (because your constructor is not explicit).

    Here is the corrected code

    #include <iostream>
    
    using namespace std;
    
    struct Element {
        void SetVisible(bool) { cout << "Called SetVisible on Element" << endl; }
    };
    
    struct ElementQuery {
        explicit ElementQuery(Element * e) : element(e) { }
        Element * Get() const { return element; }
        Element * element;
    };
    
    namespace A {
    
    static void SetVisible(Element * element, bool show) {
        cout << "Called A::SetVisible on Element" << endl;
        element->SetVisible(show);}
    
    static void SetVisible(ElementQuery const& element, bool show) {
        cout << "Called A::SetVisible on ElementQuery" << endl;
        SetVisible(element.Get(), show);
    }
    
    };
    
    int main() {
        Element * e = new Element();
        ElementQuery q(e);
        A::SetVisible(q, true);
        delete e;
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code snippet: #include <vector> using namespace std; void sub(vector<int>& vec) {
please consider following code #include <iostream> using namespace std; class Digit { private: int
Please let us consider following code: #include <iostream> using namespace std; union{ int i;
Consider the following code snippet: std::vector<int> v; v.reserve(100); v.insert(v.end(), 100, 5); v.erase(v.begin(), v.end()); std::cout
Consider the following short code snippet. namespace B { public class Foo { public
Consider the following 2 code snippets: Case 1: #include <iostream> int main() { int
Consider the following code Snippet Form form2 = new Form(); test(form2); form2.Show(); public void
Consider the following code snippet private void ProcessFile(string fullPath) { XmlTextReader rdr = new
Consider the following code snippet: void MyFun() { SetMe(); // more code if (..
Consider the following code snippet namespace ConsoleApplication1 { public delegate TResult Function<in T, out

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.