I was wondering if it is legal to put a pointer in a base class? And am I calling the pointer correctly. This is just a small sample of my program to get the question across.
/*
Single room security system:
status: I want to use switch to choose in menu
1 disabled
2 on
3 off
4 intrusion
5 error
*/
class sec_sys
{
public:
sec_sys();
~sec_sys();
...
void setcam(int c){camera = c;}
int getcam(){return motion;}
private:
...
int camera;
public:
...
int *cmra = &camera;
};
//here is the class constructor with default values.
sec_sys::sec_sys()
{
room = "Default Room";
window1 = 3;
window2 = 3;
door = 3;
motion = 3;
camera = 3;
}
sec_sys::~sec_sys(){}
//here would be what my attempt to dereference a pointer will look like
int main(int argc, char *argv[])
{
...
sec_sys roomone;
roomone.cmra = 2;
...
system("PAUSE");
return EXIT_SUCCESS;
}
This is just a very light simulation to practice my C++ skills. I left out some of the code to make for a smaller post. Would this type of dereferencing work? Is there a better way to do it than this even if it does work? Thank you.
You cannot initialize member variables in the class body like that (the only exception is static const integral types). You must initialize them in the class's ctor(s).
class sec_sys
{
//...
int* cmra; // can't initialize here
};
//-----
sec_sys::sec_sys()
{
//...
cmra = &camera; // initlialize here instead
}
//--------
int main()
{
//...
sec_sys roomone;
*(roomone.cmra) = 2; // cmra is a pointer, so you need to dereference it with * if you want to assign the target
roomone.camera = 2; // above line and this line have the same effect
}
Also note that when using pointers in a class, this pretty much always means you need to implement your own copy constructor and assignment operator, because the defaults will mangle your pointer.
Okay I understand now. Thanks! :o) But I don't know what line 20 of your example means exactly. I'm still new at programming.
*(roomone.cmra) = 2; // cmra is a pointer, so you need to...
I understand the dereference operator. But I don't get the () in the line. I can see how you got the roomone.cmra. Is there a name for this I can look up on the net. It's probably something only a noob would not understand. Regardless...
As a habit, I put parenthesis anywhere there's potential cause for confusion, which is pretty much anywhere there's two operators in the same line of code (with a few exceptions where it's obvious).
1 2 3 4 5 6 7 8 9 10 11 12
*roomone.cmra = 2;
// is the above the same as:
(*roomone).cmra = 2;
// or
*(roomone.cmra) = 2;
// ?
// As wmheric pointed out, it's the same as the latter, but peronsally I dont' feel anyone should have to know.
// I never bothered to memorize the order or precedence because it's a waste of time to do so. Just use
// parenthesis and it doesn't matter.
PS: On a somewhat unrelated topic... I want to stab people who do things like:
So I declare the pointer in the class declaration and initialize it in the constructor. Awesome! Thanks for the help guys( and ladies). And Microsoft compilers are giving warnings for not using () with two or more operators now-a-days. So I am slowly learning to use them. They are a large benefit for just a couple of keystrokes. :o)
~I think I am in love...
In love with C++
^^Goofy crud^^
VV VV
1 2 3 4 5 6 7 8 9 10 11 12 13
//Here's a little something for all the help! LOL
#include "goofball.h"
int main()
{
goofball lare26;
lare26.smile();
lare26.shakehand();
lare26.saythanks();
lare26.exitroom();
return 0;
}