stupid C++ compiler: cannot see class member function defined later on

Hi C++ designers,

I want to make a suggestion to C++ compiler designers to adopt one feature from Java, that is the ability to see class members defined later on. For example,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class A{
public:
 void a(){
  B b;
  b.func();
 }
 void func(){}
};

class B{
public:
 void func(){
  A a;
  a.func();
 }
};


Java can handle this with no problem, but for C++, you have to declare a() first and define it later on outside the class {}, which is very disgusting, unreadable and inconvenient.

1
2
3
4
void A::a(){
 B b;
 b.func();
}


Because of this, C++ programmers need to create additional files and headers to accommodate this. And you can only include the header but not the cpp file. Otherwise, compiler will complain on the redefinition, which is stupid, very inconvenient. Well, if a function is defined twice, if they are the same, what problem will there be??? If they are different, why not post a warning and adopt the latest version first. That's why Java is better in this aspect.

I know that C++ standards have been revised many times over the years. The newest C++11 standard supports "auto" variable definition adopting one of my previous ideas. They also imported many useful Boost library functions like boost::thread, boost::shared_ptr, etc. It's quite surprising to me that those famous designers have not learned anything useful from Java, such as this simple but efficient compiler-level feature.
I hope this can be a new feature for C++12 standard. Thanks!

Wang Xuancong
Last edited on
you have to declare a() first and define it later on outside the class {}, which is very disgusting, unreadable and inconvenient.

The advantage of defining the function outside the class declaration is that you can see the whole class declaration much more easily. This makes it easier for the class users to use it.
First of all such treads belongs to Lounge as there is no problem to solve.
Second, no C++ Standard Commitee member reads this forum, so your suggestion is not going to be seen anyway.

Third: C++ is not Java. You are trying to write Java code in C++ and having problems with it (Not to mention that your example looks extremely artificial and like bad code in general).
C++ Promotes separation of declarations and definitions. This is C++ way, ODR.
C++ compiler does not gets to see multiple definitions, it is a linker problem. Linker could not say if they are different or same because it does not analyse resulting code.
Moving multiple definition recognition in compile time is not possible either as it would disallow separate/parallel compilation feature. (There is a way to allow functions to be defined several time btw.)

Also you should consider backward compatibility. It is important for code written earlier to work in new language revisions.

C++ sacrifices convinience for tight control and efficiency. Think manual transmission vs automatic.

closed account (48T7M4Gy)
#include and separate files for classes overcomes this OP problem?
> such treads belongs to Lounge as there is no problem to solve.
http://www.cplusplus.com/forum/ has a description for each sub-forum. Note that `problem' is not used
Lounge is meta and not c++
I think that is fine here.


> C++ compiler does not gets to see multiple definitions,
The compiler does see redefinitions in the same translation unit.
That's one of the reason to not include source files.


> compiler will complain on the redefinition, which is stupid, very inconvenient.
> Well, if a function is defined twice, if they are the same, what problem will there be???
that the compiler must analyze the code to check that they are in fact the same

> If they are different, why not post a warning and adopt the latest version first.
that's an awful solution, ¿why not use the first? ¿how do you even know the order of the functions?
1
2
3
4
5
6
7
8
#include "mylib" //provides `foo()' and `bar()'
int foo(){ //redefining foo()
   return 42;
}

int main(){
   bar(); //¿which foo() should use?
}


The other thing is inertia. Current c++ programmers should realize that a redefinition should never happen, as it would indicate some error on the setup
Topic archived. No new replies allowed.