C++ question

Go To Last Post
17 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What does this do, in a cpp file?

class xyz;

where xyz is defined in on of the include files.

It doesn't create an instance of xyz.

confused

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This is called forward declaration: http://en.wikipedia.org/wiki/For...

:: Morten

 

(yes, I work for Atmel, yes, I do this in my spare time, now stop sending PMs)

 

The postings on this site are my own and do not represent Microchip’s positions, strategies, or opinions.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

An example from cplusplus.com:

//foo.h
#include "bar.h"

class Foo{
    Bar a;
};

//bar.h
class Foo;

class Bar{
    Foo &a
};

//foo.cpp
#include "foo.h"

//bar.cpp 
#include "foo.h"

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It is the same concept as:

int foo(); //Declaration

...

int foo() //Definition
{
   ...
}

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

It is the same concept as: [...]

With the difference that while a function prototype reveals everything about the functions interface a forward declaraton of a class does not reveal everything abouth the interface of the class (public and protected members).

It's the same concept on the level of "Hey, compiler! There will be something called X defined eventually...".

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

on the original post, I was confused about the coding of
class foo;

rather than
#include "foo.h"

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Typically you would forward declare a class in a header file so that you could use it in other declarations. e.g.

class foo;

int someFunc(foo *a_foo);

Then in the .cpp file you include foo.h so that you can actually dereference the pointer in the definition of someFunc().

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

In the post just before this post, why would you say
class foo;

instead of
#include // which defines class foo.

I'm just not getting it.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Because the .h file that you put "class foo;" into only needed to know that the class existed and not any of the specifics on how the class was defined. In the example that I gave, you are passing a pointer to a function. The declaration is only the signature which only needs to know that it takes a pointer to a class named foo. No other knowledge of how foo is implemented is needed. Only when a file actually calls the function or defines the implementation of the function does it need to know the full interface of that class. But this is nothing specific to C++. If you had a struct is C, you could do the same thing.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You need a forward declaration when class A accesses class B and class B accesses class A. Otherwise each header file includes the other and this will continue forever or until the compiler says enough already and aborts. As I recall, Microsoft's compiler quits after loading 50,000 header files.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

But that situation can be taken care of by header guards. And depending on what is being done in the headers, a forward declaration alone might not be enough.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You don't even need to involve inclusion of header files in this. The following (sketchy, untested) example will be enough:

class B;   // What would happen to the code that follows if you remove this forward declaration

class A
{
private:
   B * pb;
public:
   A::A()
   {
      pb = new B;
   }
};

class B
{
private:
   A * pa;
public:
   B::B()
   {
      pa = new A;
   }
};

A a;
B b;

int main(void)
{
   return 0;
}

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I don't know the answer.

I do like to see this:
A a;

I use the same name for the type and the instance but capitalize the type. If there are multiple instances I add a descriptive suffix.

Similarly I use a_p as the name of the pointer to the a instance.

Of course we use A and a for illustrative purposes. In actual code the name should be descriptive.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

By the way you don't need to use the stand alone "class A".

These accomplish the same thing:

class A;
A* a_p;
class A* a_p;
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

I don't know the answer.

C++ compilers like C compilers are "one pass". They don't looka ahead (very far, I add cautiously before someone yells "LALR(1)!"). When you reference something it has to already be known. In my example the definition of the class A contained a pointer to something of type B, but the definition of type B comes after the definition of class A. This means that if the

class B;   // What would happen to the code that follows if you remove this forward declaration

was removed then the compiler would barf when reaching

class A 
{ 
private: 
   B * pb;      // <----- THIS LINE!

At that point B is not something that is known to the compiler.

"Then put the definition of B first" you say? The same problem arises since a B references (well, has a pointer to) type A.

To solve this, there must be a way of saying ''Hey compiler, you will know the details of it later, but right here and now I'll let you know up-ahead that there will be a type called "B" ''. This is what the

class B;

does.

BTW: Someone compared it to a C function prototype. After thinking about it, I don't believe that such a comparison holds all the way.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yeah but I don't need to know. ;) I don't do that stuff. If once in a blue moon I do it, I'll figure it out then. I work on the "need to know" basis.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well, now I am confused :

Quote:
What does this do, in a cpp file?
Quote:
Yeah but I don't need to know.
Did I missed something?

"As simple as possible, but not simpler"

GUI Framework for Atmel Xplained Pro