pointers in class

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
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.
Yes this is legal, except you're doing it wrong.

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).

ie:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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...

Thanks for your reply! :o)
Last edited on
But I don't get the () in the line


If I remember correctly, . has a higher precedence than *, why not remove the () and try ;)
[edited] oh, did I forget their evaluation directions...

Nevertheless, parentheses make things clearer.
...or to remind yourself that cmra is a pointer while camera is an int.
Last edited on
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:

*foo++;

grah die!!!!!


edit: whoops got your names backwards!
Last edited on
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;
}
Topic archived. No new replies allowed.