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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:26:28+00:00 2026-05-23T18:26:28+00:00

Consider this code: #include<iostream> using namespace std; class Wilma { public: static int i;

  • 0

Consider this code:

#include<iostream>
using namespace std;
class Wilma
{
    public:
        static int i;
        Wilma()
        {
            cout<<"\nWilma ctor\n";
            cout<<"\ni::"<<i<<"\n";
        }
};
class Fred
{
    public:
        Fred()
        {
            cout<<"\nFred ctor\n";

        }
        static Wilma wilma_;
};
int Wilma::i=44;//------------- LINE A
int main()
{
    int a=0;
    Wilma::i=a;//---------- LINE C
    Wilma w;
    Fred::wilma_=w;//---------- LINE B

}

here line A explicitly defines the static int a of Wilma class.(commented out to cause linker error)
and without which the linker gives undefined reference error.(because Wilma::i is actually being used,if i dont use it no linker errors are there.)

Same should be true for static Wilma wilma_ of Fred class,i.e it should be explicitly defined aswell..because it is also being used in the code at line B.
But thats not the case,no linker errors for Fred::wilma_ if its not been explicitly defined.
why?
Tested on gcc 4.5.2

EDIT:
I somehow got another doubt about this…

LINE C and LINE B both are trying to use static objects of a class,int Wilma::i and Wilma Fred::wilma_ respectively.
But only a definition for int Wilma::i is mandatory?

Why isnt Wilma Fred::wilma_; mandatory?

I understand the answer that the line B is a no-op. but same can be said about line C too??

  • 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-23T18:26:28+00:00Added an answer on May 23, 2026 at 6:26 pm

    Wilma has no non-static fields. Fred::wilma_=w; doesn’t do anything.

    edit

    If there are no non-static members – there’s no copy. Basically the assignment was a no-op and might just been optimized out by the compiler, and the linker never saw it. Adding a non-static member made the copy to be an actual operation that referenced the static variable, thus the compiler couldn’t optimize it out and the linker saw it.

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

Sidebar

Related Questions

Consider this code #include <iostream> #include <cstdio> using namespace std; class Dummy { public:
Consider this: #include <iostream> using namespace std; class A{ protected: void some_function(int params) {
Consider the following code: #include<iostream> using namespace std; class sample { int a; int
please consider following code #include <iostream> using namespace std; class Digit { private: int
Consider the sample code below: #include <iostream> using namespace std; class core { public:
Consider this code: #include <iostream> using namespace std; int main() { bool lock =
Consider the following sample code below: #include <iostream> using namespace std; class base {
Please let us consider following code: #include <iostream> using namespace std; union{ int i;
Please consider this code: #include <iostream> template<typename T> void f(T x) { std::cout <<
Consider this code: #include <iostream> template<class C> struct time { }; int main() {

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.