tricky stuff...

1) ok so im reading this book and the author does this
 
typedef void (*functionType) (TreeItemType& anItem);


i thought that typedef was used to define synonyms for existing types. in that statement it seems that he is defining a pointer to a function that takes a reference to an item of treeitemtype, BUT WHERE DOES HE DEFINE THE SYNONYM HE WANTS TO USE?

2) also this does not make sense to me
 
virtual bool isEmpty() const;


what is that const doing back there, what does it mean? and i thought that you were suppose to type const in the beginning not the end.

1) The name of the function pointer in that case is "functionType". It's in a weird location (IMO).

2) It means the function is a const function, meaning that it doesn't modify the classes internal variables. In short, you can use it with a const version of the class.
@firedraco but i thought that for typedef the first parameter is the existing data type and the second one is the synonym.
also if "functionType" is supposed to be the name of the pointer, then how come he does:
 
virtual void preordertraverse(functionType visit)

so if functiontype is the name of the pointer, this how come he can do the above?


also is virtual bool isEmpty() const; same as virtual const bool isEmpty();??/?
virtual void preordertraverse(functionType visit)

I don't what's unclear here, since you typedef the pointer to a function you can now use it by its name which is functionType instead of virtual void preordertraverse( void (*functionType) (TreeItemType& anItem) )



also is virtual bool isEmpty() const; same as virtual const bool isEmpty();??/?

No. the second one says, isEmpty is a virtual function that takes no argument and it returns a constant boolean

Hope that helps
@blackcoder41, it's tricky for me because i thought that typedef was used like this :typedef existing_type new_type_name
according to the tutorial on this site. however, it did not mention using type def with functions. nevertheless i just found an article about it and it's much more clear now.

about the const, i thought u had it type it before, not after?

does this mean i can do something likeint x = 15 const since const int x = 15 is legal?
Last edited on
does this mean i can do something like int x = 15 const since const int x = 15 is legal?No, Remember that 15 is a literal, since it is a literal then it is already a constant.

http://cplusplus.com/doc/tutorial/constants/

this code is legal though.
int const x = 15;
Topic archived. No new replies allowed.