3

Background:

I'm currently working my way through the edX | Intermediate C++ course. Although the course is being led by Microsoft, it did not state that you would need to use their IDE Visual Studios to complete successfully. I only have a Mac, so I did the introduction course using Xcode with no worries at all.

Question:

During the first module of the intermediate course, it states you can declare a class inside your header as static like so:

// Math.h
// Header file for the Math class

#pragma once

// Math class definition
static class Math
{
    public:

    // given base and exponent, calculate value
    static int Math::pow(int base, int exp);

};

Xcode flagged errors saying that:

'static' is not permitted on a declaration of a type

According to a previous SO question asked here, this is not possible to do but I'm unsure if this has changed since the question was answered in 2008 or maybe this is compiler specific/VS feature which has been added by Microsoft to confuse people.

Community
  • 1
  • 1
Kitson88
  • 2,889
  • 5
  • 22
  • 37
  • 1
    If you want only public static member functions in a class, then you could just use a `namespace` instead. As for your problem, it have never been valid to add linkage specifiers like `static` to classes or structures in C++. – Some programmer dude Feb 27 '17 at 08:29
  • Or like with Java just use a private constructor and static methods, you don't have to define the class as static. – xander Feb 27 '17 at 08:31
  • Thanks both. I just did a little Google and can see what your saying. The main issue is that I'm guessing this is not possible with C++? And if so, why are Microsoft teaching this? – Kitson88 Feb 27 '17 at 08:32
  • Regarding Microsoft comment....is this a Visual Studio feature? – Kitson88 Feb 27 '17 at 08:33
  • @Kitson88 Visual Studio is "just" the IDE, it depends on the non standard compiler features, like MSVCC (Microsoft Visual C++ Compiler) has a `static class`compiler extension, maybe to ensure compatibility with managed visual C++ (and C#) – xander Feb 27 '17 at 08:37
  • 4
    It could be a C++/CLI feature? C++/CLI is an extended variant of C++ with .NET and many non-portable and non-standard features. – Some programmer dude Feb 27 '17 at 08:37
  • @xander Understand that IDE is just the IDE. I was wondering whether it was some for of shortcut-syntax rather than compiler based. – Kitson88 Feb 27 '17 at 08:41
  • @Someprogrammerdude Thanks. – Kitson88 Feb 27 '17 at 08:45
  • 1
    Please note that also the `Math::` qualifier in `Math::pow` declaration is also extraneous, which should also result in a compilation error. – Fatih BAKIR Feb 27 '17 at 09:13
  • @FatihBAKIR Yeah i noticed this when I removed the static keyword from class declaration. I've changed it to `static int pow(int, int);` which works perfectly. – Kitson88 Feb 27 '17 at 09:17
  • @Kitson88 - If you want to learn *real* C++, you shouldn't use a tutorial for the very different language [C++/CLI](http://stackoverflow.com/tags/c%2b%2b-cli/info). The name really **is** something *"which has been added by Microsoft to confuse people."* – Bo Persson Feb 27 '17 at 12:24
  • @BoPersson Understood. It stated it was an **Intermediate C++** course so felt it was the logical step. – Kitson88 Feb 27 '17 at 12:31
  • @Kitson88 - C++/CLI is "intermediate" in that it can be used to glue together native C++ with .NET code in other languages. But if you are on a Mac, you will have no use for that. – Bo Persson Feb 27 '17 at 12:38
  • @BoPersson at the moment, I'm just trying to learn and understand the basics of C++ along with common uses of the Standard Library. I come from a PHP background so as I'm sure you can appreciate, I have an uphill battle ahead of me. Understandable that learning to use WinAPI to communicate with the Mac OS would be pointless but the course is literally covering the foundation of C++ (granted incorrectly) so I suppose I will look for another source and hope I don't make the same mistake again. Thanks for your help on this. – Kitson88 Feb 27 '17 at 12:56

2 Answers2

3

static in this context isn't valid. An alternative is all-static members, but the class itself isn't valid.

A similar use of the syntax would be:

static class Math
{

    public:

    // given base and exponent, calculate value
    static int Math::pow(int base, int exp);

} math;  // <---- note object

in which case the object math is static, not the class itself.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • Thanks for this. I think it would make more sense (and easier to read) to remove the static keyword and just make the methods I need static static. – Kitson88 Feb 27 '17 at 08:56
1

Just remove the static keyword before class. Everything in the exercise will still make sense exactly in the same way.

ACosta
  • 91
  • 2
  • 5