- I don't understand this syntax for typedef and structs - 3 Updates
- boost::lambda in std::map - 2 Updates
- No public class members? - 1 Update
- Available C++ Libraries FAQ - 1 Update
agent@drrob1.com: Dec 15 05:45PM -0500 Hi. I'm new to c and c++. I am coming from the world of Modula-2 and Ada and I'm trying to understand the following syntax typedef struct _win_border_struct { chtype ls,rs,ts,bs,tl,tr,bl,br; } WIN_BORDER; typedef struct _WIN_struct { int startx, starty, height, width; WIN_BORDER border; } WIN; I am conceptualizing a struct as a RECORD type in Modula-2 and Ada. Typedef is the modula-2 equivalent of TYPE identifier = From reading what I could, I understand that the identifier that follows struct is the name of the type, and the identifier that follows the closing } is the name of a variable/object of this type. But the 2nd typedef struct does not make reference to a _win_border_struct, but instead uses the WIN_BORDER identifier. So my question is, what is the difference between the identifier that follows struct and the one that follows the closing }? Thx Rob |
Ian Collins <ian-news@hotmail.com>: Dec 16 12:05PM +1300 > Hi. I'm new to c and c++. Which one are you intending to use? The distinction is important. > { > chtype ls,rs,ts,bs,tl,tr,bl,br; > } WIN_BORDER; In C++, the typedef is unnecessary clutter, just write struct WIN_BORDER { chtype ls,rs,ts,bs,tl,tr,bl,br; }; Note many coding standards reserve all cast for macro names, not class/struct names. In C, things are somewhat more complex, struct declarations live in their own pseudo namespace and a typedef is required to map them into the "global" namespace (C doesn't actually have namespaces, but this is the easiest way to describe the rules). If you were to use your original struct definition above, you would need to explicitly name WIN_BORDER as a struct: struct _win_border_struct border; Adding the typedef allows you to write WIN_BORDER border; The "typedef struct" syntax is a shorthand for struct _win_border_struct { chtype ls,rs,ts,bs,tl,tr,bl,br; }; typedef struct _win_border_struct WIN_BORDER; -- Ian Collins |
Christopher Pisz <nospam@notanaddress.com>: Dec 15 05:17PM -0600 > follows struct and the one that follows the closing }? > Thx > Rob You will often see C programmers write this in C++ projects: typedef struct _win_border_struct { chtype ls,rs,ts,bs,tl,tr,bl,br; } WIN_BORDER; because they failed to make the distinction between the two. Don't be one of those. Make a distinction between writing C and writing C++. This is one of the differences between the two. If you are using a C++ compiler then write: struct WIN_BORDER { chtype ls,rs,ts,bs,tl,tr,bl,br; }; You will also see the former in libraries written by C programmers. Like the Windows API, because it is largely C and began long long ago. As to the reason that was needed in C and not in C++ give a read at: the section titled "More Peculiarities from Unification" on http://www.drdobbs.com/c-theory-and-practice/184403396?pgno=3 |
porparek@gmail.com: Dec 15 06:50AM -0800 Hi I would like to implement my own comparator for std::map using boost::lambda (no C++11 capable compiler). I wrote my very first version of such comparator. Unfortunately I cannot compile it. I get the following compilation error: 'boost::lambda::{anonymous}::_1' cannot appear in a constant-expression 'boost::lambda::{anonymous}::_2' cannot appear in a constant-expression Please help me to overcome this error. thanks in advance #include <map> #include <string> #include <boost/lambda/lambda.hpp> using namespace std; using namespace boost::lambda; int main(void) { map<string, string, (_1 < _2)> myMap; return 0; } |
"Öö Tiib" <ootiib@hot.ee>: Dec 15 08:54AM -0800 > map<string, string, (_1 < _2)> myMap; > return 0; > } That is because you are mixing the values and the types of the values up. As result the compiler does not understand what you want to do. Perhaps you should try it step-by-step? DISCLAIMER: I have no compiler nearby so I can't guarantee it does not have typos or something but it must illustrate the idea: // 1) Should such code work?: typedef boost::function<bool(std::string const&, std::string const&)> Comparator; // what happened on above line? // 2) After it should that work?: Comparator c = _1 < _2; // what happened on above line? // 3) Now ... does that work?: typedef std::map<std::string, std::string, Comparator> StrToStr; // what happened on above line? // 4) So ... does that work?: StrToStr myMap(c); // what happened on above line? When you understand these elementary steps then you can perhaps start to write more complex lines of C++: int main() { map<string, string, function<bool (string const&, string const&)> > myMap(_1 < _2); return 0; } I myself avoid writing such complex lines. It looks too lot like cryptic garbage. |
Juha Nieminen <nospam@thanks.invalid>: Dec 15 08:53AM > int& X() { return x; } > int& Y() { return y; } Those aren't getters and setters. Those are direct access to member variables, which nullifies the entire idea. Getters and setters would be: int x() { return mX; } int x(int value) { return mX = value; } int y() { return mY; } int y(int value) { return mY = value; } --- news://freenews.netfront.net/ - complaints: news@netfront.net --- |
Nikki Locke <nikki@trumphurst.com>: Dec 14 11:22PM Available C++ Libraries FAQ URL: http://www.trumphurst.com/cpplibs/ This is a searchable list of libraries and utilities (both free and commercial) available to C++ programmers. If you know of a library which is not in the list, why not fill in the form at http://www.trumphurst.com/cpplibs/cppsub.php Maintainer: Nikki Locke cpplibs@trumphurst.com |
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page. To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com. |
No comments:
Post a Comment