26

I intended to call a private class member function, but by a copy&paste mistake pasted the line as this function is declared in the header file:

void DebugView::on_cbYAxisEnabled_stateChanged(int)
{
    void updateAxisEnabled();
}

instead of

void DebugView::on_cbYAxisEnabled_stateChanged(int)
{
    updateAxisEnabled();
}

Surprisingly, the code was compiled and executed. However the method updateAxisEnabled() was not executed.

So, why does it compile? Was here a local function declared within a method body or has void instructed the compiler to ignore whatever comes afterwards?

The compiler is Visual Studio 2008.

P.S.: I'm aware of class declaration/definition within functions, but not functions within functions in C++.

9
  • 4
    P.S.: I'm aware of class declaration/definition within functions, but not functions within functions in C++ Well, now you know that's possible too. Commented Oct 13, 2015 at 12:38
  • @deviantfan: I'm sure, that it is not legal to DEFINE a local function in C++. So what is the point to DELCARE a local function without being able to define a body for it? Commented Oct 13, 2015 at 12:41
  • @ValentinHeinitz Have you tried with g++ for example,I am curious how that would work. Commented Oct 13, 2015 at 12:43
  • 2
    The same reason you forward declare anything; telling the compiler that a symbol's definition exists elsewhere. Commented Oct 13, 2015 at 12:43
  • @RichardRublev: yes, seems to work, Tried here: gcc.godbolt.org Commented Oct 13, 2015 at 12:45

2 Answers 2

38

void updateAxisEnabled(); is a function declaration.

Sample:

#include <cstdio>

void a();
void b();

int main(void) {
    a();
    b();
    return 0;
}

void a() {
    void c(); // Declaration
    c(); // Call it
}

void b() {
    c(); // Error: not declared
}

void c() {
    puts("Hello, world!");
}
Sign up to request clarification or add additional context in comments.

1 Comment

Cool! It works. I've tried it here: gcc.godbolt.org. Good to know, that it's possible to make forward declaration in a member function. Thanks!
5

It is perfectly allowed to declare a function inside a function scope: a function may be declared in any scope.

A common mistake among C++ programmers is indeed to:

void foo()
{
    MyObject bar(); // 1
    bar.someMethod(); // 2
}

this will miserably fail to compile because line 1 is not declaring a MyObject named bar and calling its constructor explicitly; rather, it is declaring a function named bar that returns a MyObject. Thefore, there is really no object to call someMethod on.

6 Comments

This is why I like using this syntax to initialize my variables: MyObject bar{};
@Just Yes, the uniform initialization can solve this usses since C++11.
Note, however, that you can't declare a non-member function at class scope, unless you make it a friend.
@Brian: what is the syntax fo it?
@Brian Weird! One can even define a global not-member function inside class declaration scope. Seems to be another case for: mindprod.com/jgloss/unmain.html :-)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.