views:

119

answers:

1

Hi there. I've been working through Bjarne Stroustrup's "The C++ Programming Language" (2nd edition - I know I should really get a new copy but this is a library book!), and had a few questions about one of his simpler questions. In Chapter 2, when talking about Declarations and Constants, he lists a set of declarations, some of which are definitions as well. In an exercise at the end of the chapter, he challenges the reader to go back and re-write the list, this time changing all of the defined declarations to just declarations and changing all of the non-defined ones to have a definition.

I have completed this task, hopefully mostly correctly, but there were a few parts I got stuck on. I would appreciate it if anyone could take a quick look through my list, check if there are any of the original list I mis-allocated, and then check my changes - including most specifically how to declare but not define a typedef, and if my enum declaration-not-definition is right. Thanks very much to anyone who helps. I apologise since this isn't strictly a direct code question - i.e. there's no compilable code here, it's more... I dunno. But it does have code in it, so I hope that's okay.

// Original:

/*

char ch;                                                    // Definition.
int count = 1;                                              // Definition.
char* name = "Njal";                                        // Definition.
struct complex { float re, im; };                           // Definition.
complex cvar;                                               // Definition.
extern complex sqrt(complex);                               // Declaration, NOT definition.
extern int error_number;                                    // Declaration, NOT definition.
typedef complex point;                                      // Definition.
float real(complex* p) { return p->re; }                    // Definition.
const double pi = 3.1415926535897932385;                    // Definition.
struct user;                                                // Declaration, NOT definition.
template<class T> abs(T a) { return a < 0 ? -a : a; }       // Definition.
enum beer { Carlsberg, Tuborg, Thor };                      // Definition.

*/

// Definitions/Declarations switched:

/*
extern char ch;
extern int count;
extern char* name;
struct complex;
extern complex cvar;
complex sqrt(complex in) {                                  // Yes, my maths may be wrong here. Doing the actual maths from memory.
    complex out;
    out.re = (in.re * in.re) - (in.im * in.im);
    out.im = (in.re * in.im)*2;
    return out;
}
int error_number;
                                                            // No idea how to declare but not define a typedef!
float real(complex* p);
extern const double pi;
struct user { string name; int age; char gender; };         // Lets assume we include <string>, and yes, using int for age *might* be a bit wasteful, but meh.
template<class T> abs(T a);
extern enum beer;                                           // Not sure if this is right.

*/
+4  A: 

typedef complex point; is not a definition in C++, but only in C.

You also cannot, from what I know, provide a non-defining declaration for pi without changing its meaning, because the definition it shows has internal linkage, but if you put extern const double pi; you will give it external linkage (possibly conflicting with other translation units's pi names). Note that linkage rules are complicated

static int a;
extern int a; // valid, 'a' still internal

extern int b;
static int b; // invalid!

const double pi1 = 3.14;
extern const double pi1; // valid, and 'pi1' is internal

extern const double pi2;
const double pi2 = 3.14; // valid, but 'pi2' is now external

You also cannot only declare an enumeration without defining it.

I believe your other solutions are correct.

Johannes Schaub - litb
Thank you for the answer. I assume that `typedef` does not have a definition version in C++ then - it is only ever declared?
Stephen
@Stephen yes. .
Johannes Schaub - litb
In which case, thank you for your answer. I will go mark up my answers to remind myself. ^_^.
Stephen