9

I have recently finished reading the 1st Vol. of Thinking in C++ by Bruce Eckel and have now turned to applying the knowledge to some practical use.

I was recently working with static member functions and tried making the constructor static, for which the compiler was unhappy. I checked for the reason in the book but couldn't find any.

Can anyone explain why?

P.S.: After seeing some responses, I would like to mention that the confusion arises from my knowledge that C# (and Java) allows constructors to be declared as static.

banarun
  • 2,305
  • 2
  • 23
  • 40
Sankalp
  • 2,796
  • 3
  • 30
  • 43
  • 5
    First, answer what would you expect a static constructor to do? If you can't find a solid reason for the missing feature, what's the point of having it? – Luchian Grigore Jul 06 '13 at 07:36
  • I would hack around by directly calling the constructor for that class and thus creating an object. I know that static would force all the member objects to be static, but, is it the only reason? – Sankalp Jul 06 '13 at 07:40
  • You can directly call the constructor of a class without it being static. – Luchian Grigore Jul 06 '13 at 07:41
  • 4
    The problem is that I know it's allowed to declare the constructor as static in C#. Then, why is there a restriction in c++? – Sankalp Jul 06 '13 at 07:42
  • I'm not sure about C#, but I know Java allows you to have static functions that look like a class' constructor. C++ does not allow that, no function can have the name of a constructor. That doesn't stop you from having a static function with a different name that constructs an object and returns it to the caller. – juanchopanza Jul 06 '13 at 07:47
  • 5
    Guys, it's a perfectly reasonable question for someone coming from C#, stop saying it doesn't make sense or that it's unreasonable, etc. – user541686 Jul 06 '13 at 07:53
  • @Mehrdad I didn't say it's unreasonable, I asked what the expected functionality was in order to point out the C++ alternative. – Luchian Grigore Jul 06 '13 at 08:05
  • 3
    @Mehrdad The so-called "static constructor" in C# is not a constructor, it just uses constructor-like syntax to initialize the class. A "static constructor" does not, in fact make any sense. The OP mentions calling the constructor, but "static constructors" cannot be called. – Jim Balter Jul 06 '13 at 08:05
  • 1
    possible duplicate of [Static constructor in c++](http://stackoverflow.com/questions/5803953/static-constructor-in-c), [static constructors in C++? need to initialize private static objects](http://stackoverflow.com/questions/1197106/static-constructors-in-c-need-to-initialize-private-static-objects), [What is the rationale for not having static constructor in C++?](http://stackoverflow.com/questions/5301666/), [How to initialize static member char array with code](http://stackoverflow.com/questions/11093283/) – Cody Gray - on strike Jul 06 '13 at 08:42
  • 3
    @JimBalter: I think you're explaining it to the wrong person... after all, you don't need to explain to *me* what they are! A quick search will show you that as nonsensical as the name might seem to you, [that's what they're called in C#](http://msdn.microsoft.com/en-us/library/k9x6w0hc.aspx), so it's a perfectly sensible question (and terminology) for someone coming from C#. – user541686 Jul 06 '13 at 09:01
  • @LuchianGrigore: Well, when you say "if there isn't a solid reason for the feature, what's the point of having it?" you're clearly saying it's an unreasonable feature to even bother asking about. – user541686 Jul 06 '13 at 09:16
  • @Mehrdad well, it is if you ask me. :) The C# functionality of the `static` constructor isn't really consistent of what a `static` method means. – Luchian Grigore Jul 06 '13 at 09:28
  • 1
    @LuchianGrigore: *"I didn't say it's unreasonable"*; *"well, it is if you ask me"*; [goto: HENCE_MY_COMMENT](http://stackoverflow.com/questions/17500821/?noredirect=1#comment25440654_17500821). – user541686 Jul 06 '13 at 09:31
  • @Mehrdad dammit, busted. – Luchian Grigore Jul 06 '13 at 09:32
  • @Mehrdad " that's what they're called in C#" -- yes, that's what I said. My point stands and your sophism doesn't. – Jim Balter Jul 06 '13 at 10:13
  • *it's a perfectly sensible question (and terminology) for someone coming from C#.* -- not at all, because a) the OP wrote of calling the constructor and having it create an object, so doesn't understand what a C# *so-called* "static constructor" is and b) once again, they aren't constructors, regardless of what MS calls them, so it's not perfectly sensible terminology. – Jim Balter Jul 06 '13 at 10:16
  • @LuchianGrigore: LOL yeah. – user541686 Jul 06 '13 at 16:55

5 Answers5

15

The purpose of a constructor is to initialize the contents of an instance of the class.

Static methods don't have an instance associated with them.

Hence there is no such thing as a static constructor.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • 2
    Well C++ could define static constructor the same way that C# does or Java with static initialization blocks. A static constructor is not especially a non-sens by itself. – log0 Jul 06 '13 at 07:58
  • 3
    @log0 As Lincoln said, a dog only has four legs even if you call its tail a leg. Regardless of what MS calls them, C#'s class initializers are *not* constructors. A "static constructor" is indeed nonsense. C++ could in the future use constructor-like syntax, or Java-like syntax, or some other syntax to provide class initializers, but whatever syntax it might use, it's still not a constructor. – Jim Balter Jul 06 '13 at 08:11
  • @JimBalter: To entertain your idea, here's some food for thought: in fact, the word "constructor" is incorrect **in all cases** in C++, C#, Java, etc. The only language that gets the terminology "right" is Python: its initializer (`__init__`) does exactly what "constructors" do in C++, C#, Java, etc. -- i.e., they initialize instance fields. By contrast, a true "constructor" would be equivalent to Python's `__new__` -- its "construction" job would be to allocate memory (perhaps via `new` in C++ or `malloc` in C) and **then** *call the initializer* (which we call the "constructor"). – user541686 Jul 06 '13 at 09:09
  • @JimBalter: So, as a concrete example, Windows's `CreateWindow` function would be a true *constructor*, and the handler of the `WM_CREATE` message that it sends would be the "initializer", or what C++ calls a "constructor". (This is precisely why wrapping `CreateWindow` is so ugly in C++, by the way -- it's because C++ doesn't let you override *construction*, but only *initialization* and memory allocation.) But I digress... the point is, this discussion doesn't get us anywhere. Like it or not, the wrong terminology has stuck, so the right thing to do is to just drink the Kool-Aid and go on. – user541686 Jul 06 '13 at 09:13
  • 1
    @Mehrdad You're off topic. Regardless of your parsing of what a "true constructor" is, static initialization isn't a constructor. – Jim Balter Jul 06 '13 at 10:11
1

The language as such does not provide such functionality but it can be simulated indirectly. See this answer for more details. Not that I am really sure why you would ever need to do such a thing.

Community
  • 1
  • 1
ojblass
  • 21,146
  • 22
  • 83
  • 132
0

The constructor member function constructs the object as specified, using an existing allocation -- i.e. this is present.

static member functions specify no storage to an object, so there is no associated instance to construct -- i.e. this is absent. Therefore, you cannot specify a static constructor.

Perhaps you are looking for a named static member function which returns an instance by value:

class t_object {
public:
  static t_object Func(…optional parameter list…) {
    t_object result(…ctor parameters…);
    …initialize result…
    return result;
  }
  ...
};
justin
  • 104,054
  • 14
  • 179
  • 226
0

One very useful feature of C++ over C is it provides a proper way for proper initialization and clean-up when the instances of a user defined type are created so that you have a well formed object ready to start working with.

The language achieves this through the mechanism of constructors and destructors.

As you might note that the reason why constructors and destructors exist are for maintaining the instances which are created.

Now static implies or at-least is used when there is something common which all the objects can use. You use it when there is really something which is to be shared among all the instances of the class which you create.

And the interface to the static data members of the class is provided through static member functions which are mainly used on the static data members.

So, if the constructor is allowed to be made static what should it possibly mean so that the definition given to it by making it static is still on the lines of the reason why it came into picture(to properly initialize the object before you get hold of it). So, if there is no object then it doesn't make sense to have a constructor/destructor.

If you think on the above lines it doesn't make any sense to allow constructors to be static at least in this case(in C++). Hence, it is not supported by this language.

Uchia Itachi
  • 5,287
  • 2
  • 23
  • 26
0

Constructors are used to initialize the member variables of a instance of a class, or object, when that instance is created. Static methods of class, on the other hand, don't depend on an instance of the class.

Even though we are allowed to invoke a static member function using an instance and the '.' (dot) operator, it is recommended that we invoke the static members using the class name and the scope resolution operator '::'

If you want to create a static method which creates objects and returns them, you certainly may do this. But it can not be a constructor.

Caleb
  • 124,013
  • 19
  • 183
  • 272