Could someone explain some code?

G

Gactimus

Can anyone explain what the lines with the '*' by them do?

-----------

#ifndef _COUNTER_H
#define _COUNTER_H
#include <iostream>
using namespace std;

class Counter
{
public:
int count;
public:
* Counter(){count = 0;}
void operator ++() { ++count; }
int GetCount() { return count; }
void SetCount( int c ) { count = c; }
void PrintCount() { cout << "\n The count is " << count; }

};

class NewCounter:public Counter
{
public:
* void operator --() { --count; }
};

#endif
 
V

Victor Bazarov

Gactimus said:
Can anyone explain what the lines with the '*' by them do?

Lose the leading underscore on that macro name.
#include <iostream>
using namespace std;

This is a very bad idea. Try to avoid ever putting 'using' directives
in a header.
class Counter
{
public:
int count;
public:
* Counter(){count = 0;}

This one declares and defines the default constructor for this class.
void operator ++() { ++count; }
int GetCount() { return count; }
void SetCount( int c ) { count = c; }
void PrintCount() { cout << "\n The count is " << count; }

};

class NewCounter:public Counter
{
public:
* void operator --() { --count; }

This one declares and defines a pre-decrement operator for 'NewCounter'
};

#endif

V
 
G

Gactimus

Lose the leading underscore on that macro name.


This is a very bad idea. Try to avoid ever putting 'using' directives
in a header.


This one declares and defines the default constructor for this class.


This one declares and defines a pre-decrement operator for 'NewCounter'

Thank you.
 
P

Puppet_Sock

Gactimus wrote:
[code help snipped]

You should get yourself a copy of "Accelrated C++"
by Koenig and Moo. Get yourself on the right path
early in your C++ studies. After you've got through
that, you should sample the books with good reviews
at www.accu.org.
Socks
 
G

Gernot Frisch

Gactimus said:
Can anyone explain what the lines with the '*' by them do?

-----------

#ifndef _COUNTER_H
#define _COUNTER_H
#include <iostream>
using namespace std;

class Counter
{
public:
int count;
public:
* Counter(){count = 0;}


That is the Constructor - look in a C++ manual - any C++ manual
void operator ++() { ++count; }
int GetCount() { return count; }
void SetCount( int c ) { count = c; }
void PrintCount() { cout << "\n The count is " << count; }

};

class NewCounter:public Counter
{
public:
* void operator --() { --count; }

that is the -- operator for the class NewCounter:


NewCounter cnt;
--cnt; // This will call it

Again, RTFM
-Gernot
 
G

Gactimus

That is the Constructor - look in a C++ manual - any C++ manual


that is the -- operator for the class NewCounter:


NewCounter cnt;
--cnt; // This will call it

Ok in these lines:

void operator --() { --count; }
Counter(){count = 0;}
void operator ++() { ++count; }
int GetCount() { return count; }
void SetCount( int c ) { count = c; }
void PrintCount() { cout << "\n The count is " << count; }

What is the stuff in the {} brackets? What is it called? The book I have is
kind of crappy and I can't find where it discusses this.
 
V

Victor Bazarov

Gactimus said:
Ok in these lines:

void operator --() { --count; }
Counter(){count = 0;}
void operator ++() { ++count; }
int GetCount() { return count; }
void SetCount( int c ) { count = c; }
void PrintCount() { cout << "\n The count is " << count; }

What is the stuff in the {} brackets? What is it called? The book I have is
kind of crappy and I can't find where it discusses this.

It's called "the body of the function".
 
F

Frank Looper

#include said:
This is a very bad idea. Try to avoid ever putting 'using' directives
in a header.
Isn't that the standard? It's at least being taught as such...

Frank
 
V

Victor Bazarov

Gactimus said:
Actually, I think I see now.

I am not sure I'd label it "more efficient". And, of course, I am not
sure I understand what it is you see, but that doesn't matter.

V
 
J

Jonathan Mcdougall

Frank said:
Isn't that the standard?

The standard what?
It's at least being taught as such...

Change your teacher/book.

Unfortunately, I don't see the post you are a answering to, so I am
assuming there was a using declaration in a header.

Putting a using declaration in a header allows for side-effects. For
example, if I include <a_math_library.h>, I assume I'll get some
math-related functions, not the whole namespace std (or whatever
namespace for that matter). Just look :

# include <list>

// the name 'list' is not recommended, but shit happens
//
class list
{
// ...
};

int main()
{
list my_list;
std::list a_std_list;
}

Now that works. But now we got that new and cool math library and we
include it

# include <list>
# include <a_math_library.h>

// same thing..

but that header begins with

# include <cmath>
using namespace std;

That means our code will break beacuse we included an unrelated header.
That's an unpleasant side effect.

What's more, the usual warning about using declarations also applies:
they defeat the very purpose of namespaces, which is to avoid name
clashes. Using directives are better, using directives at function
scope in implementation file are the best.

# include <iostream>

int main()
{
using std::cout;

cout << "Here we are.";
}


Jonathan
 
E

E. Robert Tisdale

Gactimus said:
Can anyone explain what the lines with the '*' by them do?

-----------

#ifndef GUARD_COUNTER_H
#define GUARD_COUNTER_H 1
#include <iostream>
// using namespace std;

class Counter {
private:
int count;
public:
Counter(void): count(0) { } // default constructor
Counter& operator++(void) { ++count; return *this; }
int GetCount(void) { return count; }
Counter& SetCount(int c) { count = c;
return *this; }
friend
std::eek:stream& operator<<(std::eek:stream& os, const Counter& c) {
return os << c.count;
}
 
V

Victor Bazarov

Jonathan said:
[...]
What's more, the usual warning about using declarations also applies:
they defeat the very purpose of namespaces, which is to avoid name
clashes. Using directives are better, using directives at function
scope in implementation file are the best.

# include <iostream>

int main()
{
using std::cout;

cout << "Here we are.";
}

It's vice versa.

using namespace std;

is a using _directive_

using std::cout;

is a using _declaration_. So, using _declarations_ are better.

Just so we're clear on that...

V
 
D

Default User

Frank said:
Isn't that the standard? It's at least being taught as such...


No, because then everyone who includes your header gets all of
namespace std thrust into the global namespace, which might not be what
they want.

Such using declarations should only be in implementation files. Header
should just explicitly scope items from std.




Brian
 
G

Gactimus

Can anyone explain what the lines with the '*' by them do?

-----------

#ifndef _COUNTER_H
#define _COUNTER_H
#include <iostream>
using namespace std;

class Counter
{
public:
int count;




public:
* Counter(){count = 0;}
void operator ++() { ++count; }
int GetCount() { return count; }
void SetCount( int c ) { count = c; }
void PrintCount() { cout << "\n The count is " << count; }

If this section was protected, how would one access it using a friend?
 
D

David White

Gactimus said:
So is it just a more efficient way of writing a function instead of
writing a prototype and a seperate body?

More efficient to write anyway, and perhaps to read, though maybe not if
there's too much of it. Also, a function whose body is in the class
definition is also inline, i.e., the same as putting the body outside, but
with the 'inline' specification.

DW
 
F

Francis Glassborow

Default User said:
No, because then everyone who includes your header gets all of
namespace std thrust into the global namespace, which might not be what
they want.

But, unless I have completely lost track, we were not discussing a
header (I have never come across a header with main() in it.
Such using declarations should only be in implementation files. Header
should just explicitly scope items from std.

Much better is to actually understand what using declarations and using
directives do.
 
J

Jonathan Mcdougall

Victor said:
using namespace std;

is a using _directive_

using std::cout;

is a using _declaration_. So, using _declarations_ are better.

Just so we're clear on that...

Whoaa, I've been saying that for years and I never noticed. Sorry for
the troubles.


Jonathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top