Does Class/Function Order Matter in C++

Does class/function order matter in C++?

Yes, you must at least declare the class/function before you use/call it, even if the actual definition does not come until afterwards.

That is why you often declare the classes/functions in header files, then #include them at the top of your cpp file. Then you can use the classes/functions in any order, since they have already been effectively declared.

Note in your case you could have done this. (working example)

void hi();    // This function is now declared

struct A; // This type is now declared

struct B {
A* a; // we can now have a pointer to it
};

int main() {
hi();
return 0;
}

void hi() { // Even though the definition is afterwards
cout << "hi" << endl;
}

struct A {}; // now A has a definition

Does the order of declaring functions and methods in C++ matter

The order does matter. If you reference a function which has not been declared (just the signature and return type, no implementation) then the compiler will throw an error. The definition of your function can wait until link time. AFAIK, there is not implicit declaration in C++.

Usually you put the declarations of your functions in header files. Traditionally, the symbols exported by a translation unit (usually a stand-alone source file e.g., hello.cpp) will be made available through a similarly-named header file (e.g., hello.h). The implementation then follows in the source file. Every translation unit can then include header files from other translation units (e.g. other.h).

Every translation unit gets compiled individually (i.e. a source file such as hello.cpp; all #include preprocessing statements are replaced by the actual contents of the files to be included). At link time, the implementations of the functions in different translation units get linked together. If this linking step fails, then you can still encounter errors.

Why doesn't the order of methods in a class matter in C++?

For the most part, a C++ file is parsed top-to-bottom, so entities must be declared before they are used.

In your class, bar2 and b are invalid because they both make use of my_int_type, which has not yet been declared.

One exception to the "top-to-bottom" parsing rule is member functions that are defined inside the definition of their class. When such a member function definition is parsed, it is parsed as if it appeared after the definition of the class. This is why your usage of my_int_type in bar is valid.

Effectively, this:

struct foo
{
void bar()
{
my_int_type b;
}

typedef int my_int_type;
};

is the same as:

struct foo
{
void bar();

typedef int my_int_type;
};

inline void foo::bar()
{
my_int_type b;
}

Does the order of overloaded function declaration matter in c++?

Short answer is yes - the order matters. (Also, it really has nothing to do with overloads - you could rename Foo(B) to Goo(B).)

One common remedy for your particular problem is to forward-declare Foo(B):

// Forward declaration
void Foo(B);

void Foo (A input) {
B b = B();
Foo(b); // Compiler now knows about Foo(B), so this is fine.
}

void Foo (B input) {
// ...
}

The compiler needs to know about that particular function - it must have been declared before it's use. However, it can be defined elsewhere. At link time, all the compiler output is gathered and symbols are "linked" together - the linker will figure out how to produce the right instructions to call Foo(B) from that line, or possibly inline it, etc.

There may be times where the function must be forward-declared. E.g.

void Foo() {
if (condition) Goo();
}

void Goo() {
if (condition) Foo();
}

Both Foo and Goo need to know about each other, so you might declare both before Foo()'s definition (or put them in a header, if appropriate).

Does order of method declarations in a class matter to the compiler?

My question is whether it matters where in the list of public methods I add the new declaration

AFAIK, it does not matter if the member function is regular function but it matters if it is a virtual member function. The virtual member functions in the virtual table are in a certain order. If the library has them in a different order than your .h file, you'll most likely end up calling the wrong function.

Related: Force the order of functions in the virtual method table?

Does the function declaration order matter in a header file?

Even if you're sharing the same header file, you can still come up against this issue with versioning. For example, your project might have been compiled against version 1 of the dynamic library. You then ship version 2 of the library without recompiling your project. In that situation, you need to ensure the library does not break binary compatibility.

A good list of what can and can't be done in maintaining binary compatibility is here: http://techbase.kde.org/Policies/Binary_Compatibility_Issues_With_C++#The_Do.27s_and_Don.27ts

To answer your question, you can re-order non-virtual functions but not member variables or virtuals.

Why does the order of the class members matter?

The declarations are compiled in order. It's the same reason you can't write:

int y = x;
int x = 5;

The bodies of inline functions (incl. c-tor initiailizer lists) are processed later (parsed first of course, but the names aren't looked up until after the class definition is complete) so they can refer to class members that are on later lines.

Function declaration order matters in c language or am I doing something wrong?

Except in a few exceptions, an identifier in C cannot be used before it has been declared.

Here is how to declare a function outside its definition:

// Declare randomStr function
char *randomStr(int length);


Related Topics



Leave a reply



Submit