Need C ++ code explanation

namespace Stack { struct Rep; // definition of stack layout is elsewhere typedef Rep& stack; stack create(); // make a new stack void destroy(stack s); // delete s void push(stack s, char c); // push c onto s char pop(stack s); // pop s } 

This is in the “A Tour of C ++” block, before even basic material, such as functions, loops, and basic data types ... I don’t know whether / how I should understand this code or not.

Anyway, can someone explain this? It should be part of the explanation of how to “define a stack manager with an interface”

Firstly, I don’t know specifically what typedef Rep& stack means.

+4
source share
5 answers

In short, stack is a typedef for reference to a Rep type. This means that whenever you use stack , you specify the type of "link to Rep".

In my opinion, this is strange material from the C ++ course. From what the interface looks like, we can assume that the implementation will be something like

 /* in darkcorner.cpp: */ stack create() { // make a new stack stack s = *new Rep; return s; } void destroy(stack s) { // delete s delete &s; } void push(stack s, char c) { // push c onto s s.push(c); } char pop(stack s) { // pop s return s.pop(); } 

Now, if you do the following, you violate the principle of least surprise for most C ++ programmers.

 stack p = create(); stack p2 = p; 

The principle of least surprise says this copies the stack. But in fact, stack denotes the Rep& type, which is a "reference to Rep". This means that the second line creates an alias p (a reference to what p means). It does not copy, but simply creates another link.

I recommend you: do not allow such a code. If you want to hide the implementation or layout of Rep , then follow the Pimpl idiom .

+7
source

typedef Rep& stack means stack will be synonymous with Rep& . Therefore, instead of

 Rep &r; 

You can write

 stack r; 

Update: Rep &r means that r is a reference to an object of type Rep .

For instance:

 Rep rep; // object of type (class) Rep Rep &ref = rep; // reference to rep 

BTW. I don't like the code you posted.

  • typedef is something just to make a link, not a good idea; it should be clearly visible that something is a pointer or reference, because then it behaves differently than it looks.

  • push() and pop() must be methods of the Rep class (or better stack ), or is it rather C code than C ++ :)

  • memory management; you must ensure that destroy() is called for every stack you find from create() . Typically, smart pointers are used to ensure proper memory management.

+3
source

You are in a namespace called "Stack" to keep all the characters you declare global and potentially conflicting with other characters.

struct Rep is a declaration that says there is a type of structure called Rep, properly defined somewhere else, and one that you will reference, even if you don’t know exactly what layout it has here.

typedef Rep& stack makes the stack a literal synonym for Rep &, as Messa already answered. Therefore, everywhere you write "stack" in the same way as writing "Rep &". Rep & means a reference to a structure of type Rep. The size of the link will be known even without knowledge of the full definition of Rep, so a direct declaration is not a problem.

The four remaining lines declare functions that are supposed to perform the operations suggested by the names and, as indicated in the comments.

+1
source

This is a way to show C-encoders how C ++ objects are formed; namespace Stack { struct Rep; } namespace Stack { struct Rep; } will provide you with a Stack::Rep element, which is similar to executing class Stack { struct Rep;} . If you are new to C and namespaces, this seems like a strange example at the beginning of the book.

In any case, this is my best guess, when I first saw it after using C for a while, I realized: "Oh, that's how objects came about. These are namespaces with added pleasure in the compiler!"

+1
source

typedef Rep& stack means that in the Stack namespace, if you use Stack , it means its type is Rep& . This is like assigning a type to a word.

0
source

All Articles