Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

what is a tentative definition ? 6

Status
Not open for further replies.

nuct

Programmer
Sep 5, 2001
103
Hi, im reasonably new to c++ and am still in the research stage. Im currently looking into storge classes and things like that and I've come across something called "tentative definitions", and for the the life of me I can't see the point of them. Apparently there for declaring an identifier so that you can refer back to it before its defined, or something, but you can do this with normal declarations can't you??. Could anyone shed any light?

Thanks,

Simon.
 
what you're talking about sounds like forward declarations to me, but I'm not sure. If so, this is what a forward declaration is. Say that you have a class called CCar, and it will have a pointer to a CSteeringWheel object. Your class may look something like this:

class CCar
{
//other code ...
CSteeringWheel * pSW;
}

if you don't put a forward declaration for this before your class, your program won't compile. It looks like this with the forward declaration:

class CSteeringWheel;
class CCar
{
//other code ...
CSteeringWheel * pSW;
}

this lets the compiler know that pSW is a pointer to a class object, so don't worry about it. In your Car.cpp file you will do an '#include SteeringWheel.h', so everything will work out right. Of course, you could have just put the previous include statement in your Car.h file, but then it would be included in every place that your CCar class definition was, which you might not want. Note that forward declarations only work with pointers. If you had wanted to use an actual instance instead of a pointer, you would have had to #include SteeringWheel.h in Car.h




bdiamond
 
Consider this:

Code:
class A
{
    B* b;
};

class B
{
    A* a;
};

There's no way, by just shuffling the full definitions, that you could get this to compile.

In this case, it is necessary to forward declare one of the classes.
 
The term "tentative declaration" actually has a special meaning.
Note: Forward declared type != tentative declaration.

A tentative declaration is where you "forward declare" a variable. This is not possible in C++, as it only allows a certain variable to be declared once.

It is possible in C though...



/Per

"It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure."
 
Sorry, "tentative declaration" should read "tentative definition".

But it's still not valid C++ :)



/Per

"It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure."
 
Hmm...

Care to give a code example of that, PerFnurt?

To me, forward-declaring a variable sounds like
Code:
extern int ducks;

void add_ducks( int quack )
{
    ducks += quack;
}

int ducks = 100;
which is legal C++.

It sounds like you're describing something else, though.
 
For example, this is valid C code, but causes redefinition errors when compiled using C++
Code:
#include <stdio.h>

extern int foo;     /* definitions */
extern int foo;

int foo;            /* tentative definitions */
int foo;

int foo = 0;        /* declaration */

int main()
{
    printf( "%d\n", foo );
    return 0;
}

--
 
>To me, forward-declaring a variable sounds like...

Yes it does, doesn't it. But a tentative definition is not a forward declaration. The forward declaration is just saying "there's this var/type somewhere and that's what I'll be referring to." Tentative definition is similar, but not really the same.

An example of a tentative definition (valid C - invalid C++):
Code:
struct SomeStruct { struct SomeStruct *next };
static struct SomeStruct struct1; /* tentative definition */
static struct SomeStruct struct2 = {&struct1},
static struct SomeStruct struct1 = {&struct2}; /* "redefining" the tentative struct1 */

You can of course achieve the above in C++ with forwards declaration, but then its forward declaration not tentative definition.


/Per

&quot;It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure.&quot;
 
Oh, Salem beat me to it

Oh well....

/Per

&quot;It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure.&quot;
 
Cheers for that, if they tentative definition are from c and not c++ it means I can forget about them, and just use nice ordinary declarations, which make sense to me.

Simon.
 
Think I might have overdone it with the stars. Just being polite.
 
If it was helpful or intelligent, give them a star.

-Bones
 
>Think I might have overdone it with the stars

No problem. I forgive you. :)

/Per

&quot;It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure.&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top