473,803 Members | 4,157 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

asking for an opinion from the collective wisdom here

I was simply wondering if this was a mal-formed class header:

#ifndef _items_h_
#define _items_h_

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Item
{
public:
Item( float weight = 0, bool set_can_equip = false, bool
set_can_attack = false, bool set_can_defend = false, bool set_can_drink
= false, bool set_can_eat = false, bool set_can_use = false, bool
set_is_consumab le = false, bool set_is_magical = false );
//Below are the set and get functions for all variables in this
class
void set_weight( float weight ) { item_weight = weight; }
float get_weight() { return item_weight; }

void set_can_equip( bool set_can_equip ) { can_equip =
set_can_equip; }
bool get_can_equip() { return can_equip; }

void set_can_attack( bool set_can_attack ) { can_attack =
set_can_attack; }
bool get_can_attack( ) { return can_attack; }

void set_can_defend( bool set_can_defend ) { can_defend =
set_can_defend; }
bool get_can_defend( ) { return can_defend; }

void set_can_drink( bool set_can_drink ) { can_drink =
set_can_drink; }
bool get_can_drink() { return can_drink; }

void set_can_eat( bool set_can_eat ) { can_eat = set_can_eat; }
bool get_can_eat() { return can_eat; }

void set_can_use( bool set_can_use ) { can_use = set_can_use; }
bool get_can_use() { return can_use; }

void set_is_consumab le( bool set_is_consumab le ) { is_consumable
= set_is_consumab le; }
bool get_is_consumab le() { return is_consumable; }

void set_is_magical( bool set_is_magical ) { is_magical =
set_is_magical; }
bool get_is_magical( ) { return is_magical; }

void describe(); // simply returns description (see below)
void clear_describe( ); // clears description
void add_describe( string line_to_add );
/*Line above checks to see if description is set to either
need_set or desc_cleared, and if so description.cle ar()
then push_back( line_to_add ), else just push_back( line_to_add)*/

private: // These are stats that will be set for every item created
string need_set; // description is initialized with this
string desc_cleared; // description will be set to this after it
has been cleared
vector<stringde scription; // This is to allow multi-line
descriptions
float item_weight; // These are pretty self explanatory I think
bool can_equip;
bool can_attack;
bool can_defend;
bool can_drink;
bool can_eat;
bool can_use;
bool is_consumable;
bool is_magical;
};

#endif

but as I read along I was confused over something. The above works just
fine, but I was also wondering should the constructor be this:

Item( float weight = 0, bool set_can_equip = false, bool set_can_attack
= false, bool set_can_defend = false, bool set_can_drink = false, bool
set_can_eat = false, bool set_can_use = false, bool set_is_consumab le =
false, bool set_is_magical = false );

with a constructor body like this in the cpp file:

Item::Item( float weight, bool set_can_equip, bool set_can_attack, bool
set_can_defend, bool set_can_drink, bool set_can_eat, bool set_can_use )
{
item_weight = weight;
can_equip = set_can_equip;
can_attack = set_can_attack;
can_defend = set_can_defend;
can_drink = set_can_drink;
can_eat = set_can_eat;
can_use = set_can_use;
is_consumable = set_is_consumab le;
is_magical = set_is_magical;
need_set = "You need to set a description here!\n";
desc_cleared = "The description has been cleared.\n";
description.cle ar();
description.pus h_back( need_set );
}

or this:

Item() : float weight( 0 ), bool can_equip( false ), bool can_attack(
false ), bool can_defend( false ), bool can_drink( false ), bool
can_eat( false ), bool can_use( false ), bool is_consumable( false ),
bool is_magical( false );

and lose the entire body of the constructor in the cpp file? I will need
to pass in parameters to override the defaults when I instantiate the
objects.

Question 1: Is this a mal-formed class header?
Question 2: Which should I use for the initialization list?

Thanks in advance.
DN
--
[there are no x's in my email]

I have the right to remain silent
(and should probably use it as much as possible)
Anything I type can and will be used against me
in a court of idiocy
I have the right to be wrong
(and probably am)
If I can not furnish my own wrongness
I'm sure someone will provide it for me.
Jun 2 '07 #1
24 1231
Devon Null wrote:
I was simply wondering if this was a mal-formed class header:

#ifndef _items_h_
#define _items_h_
Yes, you are malformed right here. Per the Standard, any identifier
with a leading underscore is reserved to the implementation in the
global namespace. _items_h_ is in the global namespace.
Jun 2 '07 #2
Devon Null <th************ *@xgmailx.comwr ote:
I was wondering should the constructor be this:

Item( float weight = 0, bool set_can_equip = false, bool set_can_attack
= false, bool set_can_defend = false, bool set_can_drink = false, bool
set_can_eat = false, bool set_can_use = false, bool set_is_consumab le =
false, bool set_is_magical = false );

with a constructor body like this in the cpp file:

Item::Item( float weight, bool set_can_equip, bool set_can_attack, bool
set_can_defend, bool set_can_drink, bool set_can_eat, bool set_can_use )
{
item_weight = weight;
can_equip = set_can_equip;
can_attack = set_can_attack;
can_defend = set_can_defend;
can_drink = set_can_drink;
can_eat = set_can_eat;
can_use = set_can_use;
is_consumable = set_is_consumab le;
is_magical = set_is_magical;
need_set = "You need to set a description here!\n";
desc_cleared = "The description has been cleared.\n";
description.cle ar();
description.pus h_back( need_set );
}

or this:

Item() : float weight( 0 ), bool can_equip( false ), bool can_attack(
false ), bool can_defend( false ), bool can_drink( false ), bool
can_eat( false ), bool can_use( false ), bool is_consumable( false ),
bool is_magical( false );

and lose the entire body of the constructor in the cpp file? I will need
to pass in parameters to override the defaults when I instantiate the
objects.
You can't lose the entire body of the constructor. However to answer the
underlying question, you should use the no argument constructor and just
initialize all the variables.

Item::Item() :
weight( 0 ),
can_equip( false ),
can_attack( false ),
can_defend( false ),
can_drink( false ),
can_eat( false ),
can_use( false ),
is_consumable( false ),
is_magical( false )
{ }

At some point you will realize that your code is full of blocks that
check the various flags in this class and behave differently depending
on the setting of the flag. Then when you learn about polymorphism, you
will start writing better classes.
Jun 2 '07 #3
On Jun 3, 1:06 am, "Daniel T." <danie...@earth link.netwrote:
Devon Null <theronnights.. .@xgmailx.comwr ote:
I was wondering should the constructor be this:
Item( float weight = 0, bool set_can_equip = false, bool set_can_attack
= false, bool set_can_defend = false, bool set_can_drink = false, bool
set_can_eat = false, bool set_can_use = false, bool set_is_consumab le =
false, bool set_is_magical = false );
with a constructor body like this in the cpp file:
Item::Item( float weight, bool set_can_equip, bool set_can_attack, bool
set_can_defend, bool set_can_drink, bool set_can_eat, bool set_can_use )
{
item_weight = weight;
can_equip = set_can_equip;
can_attack = set_can_attack;
can_defend = set_can_defend;
can_drink = set_can_drink;
can_eat = set_can_eat;
can_use = set_can_use;
is_consumable = set_is_consumab le;
is_magical = set_is_magical;
need_set = "You need to set a description here!\n";
desc_cleared = "The description has been cleared.\n";
description.cle ar();
description.pus h_back( need_set );
}
or this:
Item() : float weight( 0 ), bool can_equip( false ), bool can_attack(
false ), bool can_defend( false ), bool can_drink( false ), bool
can_eat( false ), bool can_use( false ), bool is_consumable( false ),
bool is_magical( false );
and lose the entire body of the constructor in the cpp file? I will need
to pass in parameters to override the defaults when I instantiate the
objects.

You can't lose the entire body of the constructor. However to answer the
underlying question, you should use the no argument constructor and just
initialize all the variables.

Item::Item() :
weight( 0 ),
can_equip( false ),
can_attack( false ),
can_defend( false ),
can_drink( false ),
can_eat( false ),
can_use( false ),
is_consumable( false ),
is_magical( false )
{ }

At some point you will realize that your code is full of blocks that
check the various flags in this class and behave differently depending
on the setting of the flag. Then when you learn about polymorphism, you
will start writing better classes.- Hide quoted text -

- Show quoted text -
both forms can be used for ctors but the later is better when const
data members exist and the former is needed when members have to be
assigned assciated values:

int f(int,int);//A big function

class A{
public:
const int m;
int n,p;
A(int i,int j):
p(0),
m(f(i,j))//const must be initialized here.
{n=m;};/*if n is initailized in the initializer list f is called
twice which time expensive.*/
};

Jun 2 '07 #4
On Jun 2, 11:51 pm, red floyd <no.s...@here.d udewrote:
Devon Null wrote:
I was simply wondering if this was a mal-formed class header:
#ifndef _items_h_
#define _items_h_

Yes, you are malformed right here. Per the Standard, any identifier
with a leading underscore is reserved to the implementation in the
global namespace. _items_h_ is in the global namespace.
what is wrong with it?
This is an avanced technique used in proffesional libraries to prevent
probable linker errors generated by repetition.It also decreases
compile time.

regards,
FM

Jun 2 '07 #5
In article <11************ *********@h2g20 00hsg.googlegro ups.com>,
fa***********@g mail.com says...
On Jun 2, 11:51 pm, red floyd <no.s...@here.d udewrote:
Devon Null wrote:
I was simply wondering if this was a mal-formed class header:
#ifndef _items_h_
#define _items_h_
Yes, you are malformed right here. Per the Standard, any identifier
with a leading underscore is reserved to the implementation in the
global namespace. _items_h_ is in the global namespace.

what is wrong with it?
This is an avanced technique used in proffesional libraries to prevent
probable linker errors generated by repetition.It also decreases
compile time.
As he already pointed out, what's wrong is the _name_ you've used. The
technique is perfectly legitimate and allowable, but the name you've
used is not. If you change to something like:

#ifndef ITEMS_H_INCLUDE D
#define ITEMS_H_INLCUDE D

you'll be fine, because this name does NOT have a leading underscore
like the one you used.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 2 '07 #6
terminator wrote:
On Jun 2, 11:51 pm, red floyd <no.s...@here.d udewrote:
>Devon Null wrote:
>>I was simply wondering if this was a mal-formed class header:
#ifndef _items_h_
#define _items_h_
Yes, you are malformed right here. Per the Standard, any identifier
with a leading underscore is reserved to the implementation in the
global namespace. _items_h_ is in the global namespace.

what is wrong with it?
_items_h_ starts with an underscore.
This is an avanced technique used in proffesional libraries to prevent
probable linker errors generated by repetition.It also decreases
compile time.
Yes, but there are two rules:

1. Never use a name starting with an underscore in the global namespace.
2. Never use a name starting with an underscore followed by a capital
letter, or a name containing __ in _any_ namespace.

Include guards are very handy, but name them properly.

--
rbh
Jun 2 '07 #7
terminator <fa***********@ gmail.comwrote:
both forms can be used for ctors but the later is better when const
data members exist and the former is needed when members have to be
assigned assciated values:

int f(int,int);//A big function

class A{
public:
const int m;
int n,p;
A(int i,int j):
p(0),
m(f(i,j))//const must be initialized here.
{n=m;};/*if n is initailized in the initializer list f is called
twice which time expensive.*/
};
I try to avoid such micro-optimizations unless profiling has shown that
they are necessary. For this specific case, if the function has no side
effects, I expect the compiler would be smart enough to cache the return
value for the second use.
Jun 3 '07 #8
terminator wrote:
On Jun 2, 11:51 pm, red floyd <no.s...@here.d udewrote:
>Devon Null wrote:
>>I was simply wondering if this was a mal-formed class header:
#ifndef _items_h_
#define _items_h_
Yes, you are malformed right here. Per the Standard, any identifier
with a leading underscore is reserved to the implementation in the
global namespace. _items_h_ is in the global namespace.

what is wrong with it?
This is an avanced technique used in proffesional libraries to prevent
probable linker errors generated by repetition.It also decreases
compile time.
It's not at all advanced. Every header should have include guards. The
problem it solves is repeating definitions at compile time, which
produces compile-time errors, not linker errors. The problem with this
particular example, as has been pointed out in several other messages,
is the name of the guard.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Jun 3 '07 #9
On Jun 3, 8:46 am, "Daniel T." <danie...@earth link.netwrote:
terminator <farid.mehr...@ gmail.comwrote:
both forms can be used for ctors but the later is better when const
data members exist and the former is needed when members have to be
assigned assciated values:
int f(int,int);//A big function
class A{
public:
const int m;
int n,p;
A(int i,int j):
p(0),
m(f(i,j))//const must be initialized here.
{n=m;};/*if n is initailized in the initializer list f is called
twice which time expensive.*/
};

I try to avoid such micro-optimizations unless profiling has shown that
they are necessary. For this specific case, if the function has no side
effects, I expect the compiler would be smart enough to cache the return
value for the second use.- Hide quoted text -

- Show quoted text -
you pointed to the side effects ;that is exactly what I mean ,and
since there is no way for the compiler to guess the existance of such
side effects it must not censor your code-or cache the value as you
wrote.

regards

Jun 7 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1749
by: Craig S. Ugoretz | last post by:
Dear newgroup readers, I am pleased to announce the presentation of the new open source software project I have created, called the "Wisdom Seeker IDE". For a description of what open source software is about, see the web site http://www.opensource.org/. The web site for my(our) project is http://ws-ide.sourceforge.net/ws-ide.htm. It has a philosophical/political theme to it, based upon a work by the Greek philosopher Plato called...
28
2610
by: Maboroshi | last post by:
Hi I am fairly new to programming but not as such that I am a total beginner From what I understand C and C++ are faster languages than Python. Is this because of Pythons ability to operate on almost any operating system? Or is there many other reasons why? I understand there is ansi/iso C and C++ and that ANSI/ISO Code will work on any system If this is the reason why, than why don't developers create specific Python Distrubutions...
1
2108
by: Grunt | last post by:
Hi, I have been trying to put together a rotating banner. the code works but I am having a problem with the caching of the banner images. no matter what I try the page is constantly reloading the images, even worse they are not loading completely. This version includes a (vain) attempt at forcing the banner images to cache. Apart form the caching problem the scripting seems to work Any help welcomed:
250
10506
by: Sugapablo | last post by:
Just out of curiosity, while checking on a site I was working on, I decided to throw a couple of the web's most popular URLs into the W3C Markup Validator. Out of microsoft.com, google.com, amazon.com, yahoo.com, aol.com, and mozilla.org, only Mozilla's site came back "Valid HTML". So if all these places, with their teams of web developers don't seem to care, should the rest of us small time web devs concern ourselves with standards?...
258
8763
by: Terry Andersen | last post by:
If I have: struct one_{ unsigned int one_1; unsigned short one_2; unsigned short one_3; }; struct two_{ unsigned int two_1;
20
1609
by: C# Beginner | last post by:
I'm currently creating a database class, which contains a member called Open(). With this method users can open different databases. When a user tries to open a database which happens to be secured with useraccounts (username, password etc), the user should be notifed to login. Now, in my opinion their are 3 ways to cope with situation: 1. Exit the method nicely with an errorcode (eg access denied, specify accountinformation and try...
15
1215
by: Devon Null | last post by:
I was simply wondering if this was a mal-formed class header: #ifndef _items_h_ #define _items_h_ #include <iostream> #include <string> #include <vector> using namespace std;
4
1801
by: Lumpy | last post by:
Hello All, I have a question that maybe someone on here could answer. I have built websites and done other misc programming work for myself for the last couple of years. Mostly just smaller scale projects for local customers. I am nearing the end of school, I have just over two semesters left before I graduate with a degree in software engineering. Wanting to find an actual job that in the software development field, I have starting...
0
9564
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10548
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10316
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10295
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9125
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6842
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5500
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5629
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2970
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.