Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Concepts of classes and objects in C/C++

0 views
Skip to first unread message

atti...@hotmail.com

unread,
Jul 14, 2006, 1:47:50 AM7/14/06
to
how could I write a program that will declare a class point. The class
point has two private data members x and y of type float. The class
point has a parameterized constructor to initialize both the data
members i.e. x and y. The class point has a member function display()
that display the value of x and y. Create two objects p1 and p2 with
your desired data and display the values of x and y of p1 and p2. Now
create a third object p3 by using the expression p3=p1+p2 and display
the values of x and y of p3

Alf P. Steinbach

unread,
Jul 14, 2006, 2:12:10 AM7/14/06
to
* atti...@hotmail.com:

Make a better effort of conceiling the homework nature of your question,
please.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Alf P. Steinbach

unread,
Jul 14, 2006, 2:19:06 AM7/14/06
to
* Alf P. Steinbach:

> * atti...@hotmail.com:
>> how could I write a program that will declare a class point. The class
>> point has two private data members x and y of type float. The class
>> point has a parameterized constructor to initialize both the data
>> members i.e. x and y. The class point has a member function display()
>> that display the value of x and y. Create two objects p1 and p2 with
>> your desired data and display the values of x and y of p1 and p2. Now
>> create a third object p3 by using the expression p3=p1+p2 and display
>> the values of x and y of p3
>
> Make a better effort of conceiling the homework nature of your question,
> please.

Then I promise to try at least once to make a better effort at speling.

Phlip

unread,
Jul 14, 2006, 2:18:38 AM7/14/06
to
Just a note: There is no language "C/C++". We say that because, among the
C-style languages, C and C++ have very different usages and strategies.
Posters are advised to always get clear about which one they mean, before
posting, for best results!

ttique63 wrote:

> how could I write a program that will declare a class point.

By writing a program with lots of X and Y variables, and lots of unit tests.
The refactor the program, passing all the tests after the fewest possible
edits, over and over again until all Xs and Ys have migrated into the exact
kind of Point class that this program needs.

No lie; that's the most sophisticated way to do it. If you start by guessing
your program needs such-and-so Point class, and if you write the class
first, you will burden your design with decisions made without feedback.

> The class
> point has two private data members x and y of type float. The class
> point has a parameterized constructor to initialize both the data
> members i.e. x and y. The class point has a member function display()
> that display the value of x and y. Create two objects p1 and p2 with
> your desired data and display the values of x and y of p1 and p2. Now
> create a third object p3 by using the expression p3=p1+p2 and display
> the values of x and y of p3

Oh, back up a minute. This is homework, right?

Read your tutorial, write your Point class first - exactly like your
professor told you to - and don't mention _anything_ I posted here.

Then when you have taken a crack at your Point class, post it here and we
will gleefully review it. Possibly with an eye towards keeping you out of
trouble, instead of in it.

Posting your raw homework assignment, as given, is kind'a tacky. ;-)

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!


Robbie Hatley

unread,
Jul 14, 2006, 6:24:58 AM7/14/06
to
<atti...@hotmail.com> wrote in message news:1152856069.9...@h48g2000cwc.googlegroups.com...

Ummm... by reading about classes in your textbook, then putting
what you learn into practice? In the time you spent typing the
message quoted above, you could have been done with your homework
assignment already.

I'll give you one hint to get you going: declaring a class called
"Point" is going to involve writing "class Point". The syntax is
in your textbook. Read about "classes" there, please.

Once you've written your program, if it doesn't work, come back
here and copy-and-paste your program and ask for help. You'll find
that folks here are willing to help you with your homework. NOT do
it for you, though! HELP you. That means, you have to do most of
the work yourself. Get cracking.


--
Cheers,
Robbie Hatley
East Tustin, CA, USA
lone wolf intj at pac bell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/


tragomaskhalos

unread,
Jul 14, 2006, 7:19:34 AM7/14/06
to

Another tip when trying to get people to do your homework for you - try
to avoid using what is obviously the course module title as the subject
line !

Frederick Gotham

unread,
Jul 14, 2006, 8:56:19 AM7/14/06
to

class Point : public {

private float x, y;

public Point(float,float) : { x(arg1), x(arg2) }

public void Display() volatile
{
puts(FLOATING,x,y);
}

};


void main(int)
{
Point p1 = { 5.6,4.3 };
Point p2 = { 2.1,8.9 };

p1->Display(void);
p2->Display(void);

Point p3 = p1 + p2;

p3->Display(void);
}

--

Frederick Gotham

Robbie Hatley

unread,
Jul 14, 2006, 9:34:24 AM7/14/06
to
"Frederick Gotham" <fgot...@SPAM.com> wrote in message news:TvMtg.11442$j7.3...@news.indigo.ie...

Tut-tut, my dear fellow; if you're going to do the OP's homework
for him, you should at least get him to slip you a ten spot. :-)

Two things I notice, though:

1. There's some errors in there. I'm assuming you put those
in there to force the OP to research the correct syntax for
himself, so I won't say what they are.
2. The subject line is all wrong, isn't it? Classes in C? No
such thing in C. Should read: "Concepts of classes and objects
in C++" People sure do like talking about that non-existant
language C/C++. (Actually, C/C++ would always be 1, regardless
of what's in C. But I digress.)

Daniel T.

unread,
Jul 14, 2006, 9:37:36 AM7/14/06
to
In article <1152856069.9...@h48g2000cwc.googlegroups.com>,
atti...@hotmail.com wrote:

First declare a class called Point.

Then give it two private data members x and y of type float.

Then write a parameterized constructor that initializes both data
members (i.e. x and y.)

Then write a member function called "display()" that displays the value
of x and y.

Now (this is the tricky part) create a non-member function operator+
that accepts two Points as parameters and returns a Point. Have it add
the x's and y's of the two points passed in.

After you do all of the above, create two objects p1 and p2 with some
data (whatever you desire) and call display on them.

Then create a third object p3 by using the expression p3 = p1 + p2 and
call display on it.

shadow...@comcast.net

unread,
Jul 14, 2006, 10:20:12 AM7/14/06
to

Did you even bother trying to figure any of this out? This reads as if
you simply copied your homework assignment instructions verbatim. It
couldn't be more clear what you have to do here -- you have precise
instructions about how to design your program, and what steps to
follow.

Why don't you take a stab at this and come back if you get stuck. But
don't post any more questions until you have some code written. Many
people here would be happy to help, but you need to do some of the
work.

Noah Roberts

unread,
Jul 14, 2006, 11:06:18 AM7/14/06
to

Robbie Hatley wrote:

> 2. The subject line is all wrong, isn't it? Classes in C? No
> such thing in C. Should read: "Concepts of classes and objects
> in C++" People sure do like talking about that non-existant
> language C/C++. (Actually, C/C++ would always be 1, regardless
> of what's in C. But I digress.)

Well, if it is "concepts of...," then there is such a thing in C.
There is no construct "class" in the C language but you most certainly
can make them so there is such a concept.

And I would debate that there is no such language as C/C++. Perhaps
not formally defined but certainly in widespread use. Any time you see
char* really you are working in C/C++.

Victor Bazarov

unread,
Jul 14, 2006, 11:10:03 AM7/14/06
to
Noah Roberts wrote:
> [..]

> And I would debate that there is no such language as C/C++. Perhaps
> not formally defined but certainly in widespread use. Any time you
> see char* really you are working in C/C++.

OK, let's debate. When you see 'if' what language do you work in?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


Frederick Gotham

unread,
Jul 14, 2006, 11:18:25 AM7/14/06
to
Robbie Hatley posted:


> 1. There's some errors in there. I'm assuming you put those
> in there to force the OP to research the correct syntax for
> himself, so I won't say what they are.


Actually, I've decided to adopt a sadistic approach toward those who post
here blatantly demanding that their homework be done.

I posted in the heartfelt hope that the OP would copy-paste the code directly
and submit it to their teacher/lecturer/mentor/etc.

I would request, in the spirit of sadism, that you don't correct (or point
out the errors in) my posts which are a reply to people asking for their
homework to be done.

This is only my second time affecting this approach, but it's fun so far!

Here's my first one:

http://groups.google.ie/group/comp.lang.c/msg/14dc37e49bd6522d?hl=en&


--

Frederick Gotham

Victor Bazarov

unread,
Jul 14, 2006, 1:56:21 PM7/14/06
to
Frederick Gotham wrote:
> [...] I've decided to adopt a sadistic approach toward those who

> post here blatantly demanding that their homework be done.
>
> I posted in the heartfelt hope that the OP would copy-paste the code
> directly and submit it to their teacher/lecturer/mentor/etc.
>
> [...]

On a larger scale your approach accomplishes nothing, except that
unsuspecting newcomers who see your replies will think that their
questions do get answered with code and that means we'll get more
traffic asking for homework help. So, not only it doesn't help to
ward off potential do-my-homework-for-me types, it encourages more
noise in the newsgroup. As I see it, your approach is, well, bad.

Noah Roberts

unread,
Jul 14, 2006, 2:44:46 PM7/14/06
to

Victor Bazarov wrote:
> Frederick Gotham wrote:
> > [...] I've decided to adopt a sadistic approach toward those who
> > post here blatantly demanding that their homework be done.
> >
> > I posted in the heartfelt hope that the OP would copy-paste the code
> > directly and submit it to their teacher/lecturer/mentor/etc.
> >
> > [...]
>
> On a larger scale your approach accomplishes nothing, except that
> unsuspecting newcomers who see your replies will think that their
> questions do get answered with code and that means we'll get more
> traffic asking for homework help.

And when those dumbasses flunk we won't have to work with them later.

wka...@yahoo.com

unread,
Jul 14, 2006, 4:32:48 PM7/14/06
to
Daniel T. wrote:
...

> First declare a class called Point.
>
> Then give it two private data members x and y of type float.
...

It's debatable whether the x and y members should be private or public.
There is no combination of values of x and y that would result
in an object of class Point having an invalid state. So there is
no strict need for the x and y data member to be private. If
you find:

p += Point(0.5, 0.0);

to be more aesthetically pleasing than:

p.x += 0.5;

then it might make sense to make x and y private. But I don't
immediately see any other reason. Unless you're a SmallTalk
programmer wannabe who thinks it's self-evident that all
data members have an inalienable right to be private :) .

Daniel T.

unread,
Jul 14, 2006, 9:42:08 PM7/14/06
to

All that is fine if you assume that the class will always and forever,
only be implemented using two variables held in RAM representing a
cartesion coordinate system on this particular machine and in this
particular thread and no code will *ever* have to patch into objects of
the class to detect changes to one or both variables.

Now all that may very well be true especially for such a tiny little
class, however the OP's *requirement* was for the class to have "two
private data members x and y of type float". Personally, I was not in
the habit of leaving my homework requirements unfulfilled and I see no
reason to suggest others do so.

Daniel T.

unread,
Jul 14, 2006, 9:49:40 PM7/14/06
to
Frederick Gotham wrote:
> Actually, I've decided to adopt a sadistic approach toward those who post
> here blatantly demanding that their homework be done.
>
> I posted in the heartfelt hope that the OP would copy-paste the code directly
> and submit it to their teacher/lecturer/mentor/etc.
>
> I would request, in the spirit of sadism, that you don't correct (or point
> out the errors in) my posts which are a reply to people asking for their
> homework to be done.
>
> This is only my second time affecting this approach, but it's fun so far!

I like it, except can I suggest a change? Make your code completely
correct and compilable, but so obtuse/obfuscated that the OP's teacher
will know at a glance that there is no way that this student of his
created it. Just as fun as posting wrong code, but more challanging for
you.

mo

unread,
Jul 15, 2006, 1:27:14 AM7/15/06
to

If you can get the class declaration and implementation written, this
should do the trick for the rest!

#include <iostream>
#include <ifstream>
#include <cstdio>
#include <string>
#include <locale>
#include <iterator>
#include <cmath>
#include <pointclass.h> //Create this class def and you're good to go!
#include <fcntl.h>

int main() {
goto decBlock;

subBlockA:
slgdb = true;
pointarray[++i]=Point2;
if (aPx2z) goto superBlockB;
goto decBlock;

dispBlock:
std::locale euroLoc(std::locale("American_USA.1252");
std::cout.imbue(euroLoc);
if (slgdb)
goto addBlock;
goto subBlockA;

decBlock:
Point Point1();
Point Point2(32.1233,-99121243.551);
int i=12;
int j=44;
bool slgdb = false;
bool aPx2z = true;
Point pointarray[9520];
goto dispBlock;

addBlock:
while (j < 612) {
j = (j*2)-34;
j--;
}
pointarray[j] = Point1.add(Point2.getresult());
for (int k = 9520; k > 0; --k) {
if pointarray[k].equatesTo(Point1.add(Point2.getresult())) {
Point point3(pointarray[k].getXvalue,pointarray[k].getYvalue);
}
}
FILE *fs;
point3.display(fs);
return -9991;

superBlockB:
i = 4322;
pointarray[--i] = Point1;
goto dispBlock;

}

Robbie Hatley

unread,
Jul 15, 2006, 3:17:27 AM7/15/06
to
"Frederick Gotham" wrote:

> Robbie Hatley posted:
>
>
> > 1. There's some errors in there. I'm assuming you put those
> > in there to force the OP to research the correct syntax for
> > himself, so I won't say what they are.
>
>
> Actually, I've decided to adopt a sadistic approach toward those who post
> here blatantly demanding that their homework be done.
>
> I posted in the heartfelt hope that the OP would copy-paste the code directly
> and submit it to their teacher/lecturer/mentor/etc.
>
> I would request, in the spirit of sadism, that you don't correct (or point
> out the errors in) my posts which are a reply to people asking for their
> homework to be done.

Yikes!

Oh, wow. Deja vu so strong my head is litterally spinning.

I was walking to the street yesterday to a Circle K to buy milk,
when it occurred to me, "Perhaps Frederick Gotham put those errors
in that program so that the student would get in trouble with his
teacher when he handed it in as his own work? That would be pretty
sadistic; but then, cheating is a pretty horrible thing to do. I
hope he doesn't get miffed because I mentioned the fact that there
were errors in the program? But perhaps the lazy student will grab
the work and turn it in before he reads my post."

Honestly, I thought all those things!

I'll keep my mouth shut about hidden monkey wrenches from now on.
:-)

> This is only my second time affecting this approach, but it's fun so far!
>
> Here's my first one:
>
> http://groups.google.ie/group/comp.lang.c/msg/14dc37e49bd6522d?hl=en&

Ewwwww! I love the cheery "always love to help out a fellow student!"

Now here's a program that rotates ASCII characters numerically
clockwise by 13. (Not quite a ROT13, but similar concept.)
Except, it doesn't. Even if you get rid of all the compile-time
bugs (and there's lots) it'll crash at runtime. Cheating-student
fodder, perhaps:

int Main(Void)
{
chair Bob[42] = "What in Sam Hill???"
for (i = 0; i<500; +i);
{
if (Bob[i] > 127);
{
Bob[i] =- 128;
}
Bob[i] = ((Bob[i] + 13)%128);
}

if (NUL != Bob);
{
pprintf("String is: %S, Bob);
}
Return 73;
};

Frederick Gotham

unread,
Jul 15, 2006, 9:09:30 AM7/15/06
to
Robbie Hatley posted:


> Yikes!
>
> Oh, wow. Deja vu so strong my head is litterally spinning.
>
> I was walking to the street yesterday to a Circle K to buy milk,
> when it occurred to me, "Perhaps Frederick Gotham put those errors
> in that program so that the student would get in trouble with his
> teacher when he handed it in as his own work? That would be pretty
> sadistic; but then, cheating is a pretty horrible thing to do. I
> hope he doesn't get miffed because I mentioned the fact that there
> were errors in the program? But perhaps the lazy student will grab
> the work and turn it in before he reads my post."
>
> Honestly, I thought all those things!


Hehe! : )

It's not so much that I'm vehemently against cheating, but becoming
proficient at a programming language such as C++ is not something that can be
cheated at, so the OP is just putting a masochistic delay to their untimely
demise.

I've cheated at things myself in the past (mostly in school at subjects I
disdained, e.g. History), but if what I'm cheating at is anything worthwhile,
then I'll be weeded out.

I'm in college myself at the moment, and I see many fellow college-goers copy
each other's work and so forth... but I just let them get on with it because
they're only doing themselves a disservice in the long run -- which is
reflected in the drop-out rate!

If I ever have the virtue of lecturing computer programming, I'll sit down
individually with each student and say, "OK, write a program to convert
stones and pounds to kilograms", and I'll watch them do it. Might check them
for ear-pieces beforehand too. Like to see them cheat.


> I'll keep my mouth shut about hidden monkey wrenches from now on.
>:-)


Good to see I'm not the only one who thinks this is a laugh!

(P.S. I would have hoped you knew there was something up when I posted code
which was below my usual standards!)


--

Frederick Gotham

wka...@yahoo.com

unread,
Jul 15, 2006, 5:36:53 PM7/15/06
to

Daniel T. wrote:
> wka...@yahoo.com wrote:
> > Daniel T. wrote:
> > ...
> > > First declare a class called Point.
> > >
> > > Then give it two private data members x and y of type float.
> > ...
> >
> > It's debatable whether the x and y members should be private or public.
> > There is no combination of values of x and y that would result
> > in an object of class Point having an invalid state. So there is
> > no strict need for the x and y data member to be private. If
> > you find:
> >
> > p += Point(0.5, 0.0);
> >
> > to be more aesthetically pleasing than:
> >
> > p.x += 0.5;
> >
> > then it might make sense to make x and y private. But I don't
> > immediately see any other reason. Unless you're a SmallTalk
> > programmer wannabe who thinks it's self-evident that all
> > data members have an inalienable right to be private :) .
>
> All that is fine if you assume that the class will always and forever,
> only be implemented using two variables held in RAM representing a
> cartesion coordinate system on this particular machine and in this
> particular thread and no code will *ever* have to patch into objects of
> the class to detect changes to one or both variables.

If you google for "Bjarne Stroustrup", you can find and read
his arguments as to why it's not a good idea to design
classes to allow for every conceivable future use of the
class.

I think it's part of the class interface that the points are
in a cartesian coord system. But your argument for always
making data member private because it allows for future
access "side effects" does have some merit.

>
> Now all that may very well be true especially for such a tiny little
> class, however the OP's *requirement* was for the class to have "two
> private data members x and y of type float". Personally, I was not in
> the habit of leaving my homework requirements unfulfilled and I see no
> reason to suggest others do so.

In these dark times, it's good to see that some people still
follow strict ethical standards when helping others to cheat on their
homework.

Daniel T.

unread,
Jul 15, 2006, 6:20:43 PM7/15/06
to
In article <1152999413.6...@35g2000cwc.googlegroups.com>,
wka...@yahoo.com wrote:

> > Now all that may very well be true especially for such a tiny little
> > class, however the OP's *requirement* was for the class to have "two
> > private data members x and y of type float". Personally, I was not in
> > the habit of leaving my homework requirements unfulfilled and I see no
> > reason to suggest others do so.
>
> In these dark times, it's good to see that some people still
> follow strict ethical standards when helping others to cheat on their
> homework.

Helping others cheat? You might want to look at the OP and my response
again... I in no way helped anyone to cheat.

Noah Roberts

unread,
Jul 17, 2006, 11:40:08 AM7/17/06
to

Daniel T. wrote:

... but so obtuse/obfuscated that the OP's teacher


> will know at a glance that there is no way that this student of his
> created it.

I don't expect most instructors to have it in them to look to hard and
many don't know the language enough (my experience anyway). Besides,
beginner code is regularly obtuse and unreadable having major logic
leaps that are difficult to make sense of...happens because many times
they just keep changing things until it does what the assigment said
without really knowing why. Many teachers might take points for
unreadability but if it works then it passes.

I don't think your plan would work.

Daniel T.

unread,
Jul 18, 2006, 11:14:34 AM7/18/06
to
In article <1153150808....@p79g2000cwp.googlegroups.com>,
"Noah Roberts" <robert...@gmail.com> wrote:

You might be right, but I think if the teacher bothers to look at the
code and sees for example, extensive use of features not covered yet (or
even features he doesn't know) or extensive use of boolean operators, he
should be able to tell the difference. As an example, a post from
alt.comp.learn.c-c++ from February:

Dietmar Kuehl wrote:
> tiger786 wrote:
> > The program should monitor a possibly infinite stream of characters
> > from the keyboard (standard input).  If it detects the sequence "ccc"
> > it outputs a "0".  If it detects the sequence "cdc" it outputs a "1".
> > DO NOT detect sequences within sequences.  The program should exit
> > cleanly when it detects an End Of Input.
>
> The others who think that you should do your homework assignment
> alone are just spoilsports! The obvious solution to the above
> problem is rather simple:
>   #include <iostream>
>   #include <iterator>
>   #include <algorithm>
>   struct __ {
>     enum { _0 = 0x636363 };
>     __(): _() {}
>     void operator()(unsigned char _1) {
>       _ = ((_ = (_ & 0xffff) << 8 | _1) == _0 || _ == _0 + 0x100)
>         && std::cout << (_0 < _)? 0: _;
>     }
>     unsigned long _;
>   } _;
>   int main()
>   {
>     std::for_each(std::istreambuf_iterator<char>(std::cin),
>                   std::istreambuf_iterator<char>(), _);
>     std::cout << "\n";
>   }
> Of course, you should understand the program before handing it in
> because it would be a really stupid idea not to do your homework!

Several of us also posted answers that made extensive use of #define,
but the above was by far the most ingenious, IMHO.

Noah Roberts

unread,
Jul 18, 2006, 11:31:25 AM7/18/06
to

Daniel T. wrote:
> In article <1153150808....@p79g2000cwp.googlegroups.com>,
> "Noah Roberts" <robert...@gmail.com> wrote:
>
> > Daniel T. wrote:
> >
> > ... but so obtuse/obfuscated that the OP's teacher
> > > will know at a glance that there is no way that this student of his
> > > created it.
> >
> > I don't expect most instructors to have it in them to look to hard and
> > many don't know the language enough (my experience anyway). Besides,
> > beginner code is regularly obtuse and unreadable having major logic
> > leaps that are difficult to make sense of...happens because many times
> > they just keep changing things until it does what the assigment said
> > without really knowing why. Many teachers might take points for
> > unreadability but if it works then it passes.
> >
> > I don't think your plan would work.
>
> You might be right, but I think if the teacher bothers to look at the
> code and sees for example, extensive use of features not covered yet (or
> even features he doesn't know) or extensive use of boolean operators, he
> should be able to tell the difference. As an example, a post from
> alt.comp.learn.c-c++ from February:

> > struct __ {


>
> Several of us also posted answers that made extensive use of #define,
> but the above was by far the most ingenious, IMHO.

Well, if the teacher knows the language, and there is no guarantee
there, he would mark this code as erroneous. Notice that the answer is
still incorrect as the program is ill-formed.

Daniel T.

unread,
Jul 18, 2006, 12:32:44 PM7/18/06
to
In article <1153236685.6...@b28g2000cwb.googlegroups.com>,
"Noah Roberts" <robert...@gmail.com> wrote:

Why do you think it is ill-formed? You might want to look at it again.

Noah Roberts

unread,
Jul 18, 2006, 12:49:00 PM7/18/06
to

Struct definition starts with two underscores and is global; that name
is reserved. Struct contains an enum name that starts with an
underscore followed by a capitol letter; that name is also reserved.
The variable name "_" is also in the global scope and starts with an
underscore; that name is reserved. The behavior of the program is
undefined.

Daniel T.

unread,
Jul 18, 2006, 1:40:45 PM7/18/06
to
In article <1153241339.9...@b28g2000cwb.googlegroups.com>,
"Noah Roberts" <robert...@gmail.com> wrote:

> Daniel T. wrote:
> > In article <1153236685.6...@b28g2000cwb.googlegroups.com>,
> > "Noah Roberts" <robert...@gmail.com> wrote:
>
> > > > > struct __ {
> > > >
> > > > Several of us also posted answers that made extensive use of #define,
> > > > but the above was by far the most ingenious, IMHO.
> > >
> > > Well, if the teacher knows the language, and there is no guarantee
> > > there, he would mark this code as erroneous. Notice that the answer is
> > > still incorrect as the program is ill-formed.
> >
> > Why do you think it is ill-formed? You might want to look at it again.
>
> Struct definition starts with two underscores and is global; that name
> is reserved.

See [*] below.

> Struct contains an enum name that starts with an
> underscore followed by a capitol letter; that name is also reserved.

Actually it contains nothing of the sort. It contains an underscore
followed by a digit. _0

> The variable name "_" is also in the global scope and starts with an
> underscore; that name is reserved. The behavior of the program is
> undefined.

[*] I am finding conflicting information about these two points. Some
sources say _ and __ are reserved, others say that the underscores must
be followed by something (upper or lower-case letter) to be reserved.
Can I get a quote from the standard?

Noah Roberts

unread,
Jul 18, 2006, 1:46:37 PM7/18/06
to

Daniel T. wrote:
> In article <1153241339.9...@b28g2000cwb.googlegroups.com>,
> "Noah Roberts" <robert...@gmail.com> wrote:
>
> > Daniel T. wrote:
> > > In article <1153236685.6...@b28g2000cwb.googlegroups.com>,
> > > "Noah Roberts" <robert...@gmail.com> wrote:
> >
> > > > > > struct __ {
> > > > >
> > > > > Several of us also posted answers that made extensive use of #define,
> > > > > but the above was by far the most ingenious, IMHO.
> > > >
> > > > Notice that the answer is
> > > > still incorrect as the program is ill-formed.
> > >
> > > Why do you think it is ill-formed? You might want to look at it again.
> >
> > Struct definition starts with two underscores and is global; that name
> > is reserved.
>
> See [*] below.
>
> > Struct contains an enum name that starts with an
> > underscore followed by a capitol letter; that name is also reserved.
>
> Actually it contains nothing of the sort. It contains an underscore
> followed by a digit. _0

Then it's ok.


>
> > The variable name "_" is also in the global scope and starts with an
> > underscore; that name is reserved. The behavior of the program is
> > undefined.
>
> [*] I am finding conflicting information about these two points. Some
> sources say _ and __ are reserved, others say that the underscores must
> be followed by something (upper or lower-case letter) to be reserved.
> Can I get a quote from the standard?

It depends on the scope of the name. In global scope ANY name that
begins with an underscore is reserved. Other scopes anything that
begins with __ or with _ and an upper case letter. Your quote:

17.4.3.1.2 Global names

Certain sets of names and function signatures are always reserved to
the implementation:

-- Each name that contains a double underscore (__) or begins with an
underscore followed by an upper case letter (2.11) is reserved to the
implementation for any use.

-- Each name that begins with an underscore is reserved to the
implementation for use as a name in the global namespace. 165)

165) Such names are also reserved in namespace ::std (17.4.3.1)

Noah Roberts

unread,
Jul 18, 2006, 1:54:48 PM7/18/06
to

Noah Roberts wrote:

> -- Each name that contains a double underscore (__) or begins with an
> underscore followed by an upper case letter (2.11) is reserved to the
> implementation for any use.

Interestingly I believe this is an error in the standard (referring to
the reference to 2.11). 2.11 is not about characters at all but about
keywords and makes no mention of upper/lower case. 2.10 contains text
mentioning significance of case. 2.13.2 is about character literals
but also does not mention case. There is no 17.2.11. My bet is that
is supposed to be 2.10 referring to:

"An identifier is an arbitrarily long sequence of letters and digits.
Each universal-character-name in an identifier shall designate a
character whose encoding in ISO 10646 falls into one of the ranges
specified in Annex E. Upper- and lower-case letters are different.
All characters are significant..."

Daniel T.

unread,
Jul 18, 2006, 2:42:21 PM7/18/06
to
In article <1153244797.2...@b28g2000cwb.googlegroups.com>,
"Noah Roberts" <robert...@gmail.com> wrote:

Thanks.

0 new messages