4

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??

ashishsony
  • 2,537
  • 3
  • 26
  • 38

2 Answers2

3

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.

littleadv
  • 20,100
  • 2
  • 36
  • 50
  • fine.. i added int j; to Wilma.. and linker error appears now.. but i didnt get the point and how Fred::wilma_=w; isnt doing anything??..thanks for the quick response. – ashishsony Jul 12 '11 at 20:13
  • @ashishsony - added explanation to the answer. – littleadv Jul 12 '11 at 20:19
  • @ashishsony: Where is the error? Please edit your question accordingly. Are you trying to assign int j to something in the class? That is not allowed. –  Jul 12 '11 at 20:22
  • @littleadv.. just to extend the conversation,if this question is asked in an interview.. then what should one answer,because its difficult to guess how a compiler would optimize..especially in this case where optimizing or not-optimizing would end up in no-error or an error. – ashishsony Jul 12 '11 at 20:49
  • @ashishsony - I don't think the original code (with all-static Welma) would end in error regardless of optimization. If I were the interviewer, the answer I've given here would be more than satisfactory for me. Well, I gave it, after all:-) – littleadv Jul 12 '11 at 21:07
1

You declared static int i; in Wilma, but never defined it. So by adding back in Line A, it will cause Wilma::i to be defined, which is what the compiler is complaining about. So you have to define it somewhere outside the class and not inside main.

Finally, the Fred and Wilma classes are essentially empty (aside from a ctor and static class member). There is nothing to copy between them.

Edit: Based on the comments to @littleadv, you have to have an identical class if you are going to perform a copy. If you put an int j in the Wilma class but nothing in the Fred class, then it won't work because where should it put j in the Fred class?

  • my question isnt about static int i but static Wilma wilma_ – ashishsony Jul 12 '11 at 20:20
  • @ashishsony: It's hard to read your question in the first place. Nevertheless, I answered it in the second paragraph. –  Jul 12 '11 at 20:21
  • oh my my! i think you have taken this question in some other sense..i added int j to Wilma,then i am copying a temp Wilma to static Wilma wilma_(of Fred).. no question of putting int j in Fred. – ashishsony Jul 12 '11 at 20:32