Pages: [1]   Go Down
Author Topic: Dereference a pointer?  (Read 673 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Sr. Member
****
Karma: 8
Posts: 291
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

A want to get a value of public variable in an instance of a class.
I'm passing an pointer of an instance into Relay. The instance has a boolean deviceState.
Code:
class Relay : public Device
{
   public:
   Relay(uint8_t in_pin, Device *in_dependent_device) {
       dependent_device = in_dependent_device;
       Serial.print(dependent_device->deviceState);
   };
   Device *dependent_device;

}



The serial print would yield 129. I'm assuming that's an address and not the value of deviceState which should either be 0 or 1.
I've also tried
Code:
dependent_device = &in_dependent_device;
but it also yield another address.

If you can get the value that would be awesome.
thanks.

Logged

Offline Offline
Faraday Member
**
Karma: 62
Posts: 3077
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What code are you using to call this constructor method  Relay() ?
Logged

Sweden
Offline Offline
Sr. Member
****
Karma: 11
Posts: 464
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You might want to call the constructor for Device.
Code:
Relay(uint8_t in_pin, Device *in_dependent_device) :
  Device(...),
  dependent_device(in_dependent_device)
{
       // dependent_device = in_dependent_device;
       Serial.print(dependent_device->deviceState);
 };
But then I am not sure why Relay inherits from Device and has the constructor argument in_dependent_device.
You will need to post more code for more answers ;-)

Cheers!
Logged

0
Offline Offline
Sr. Member
****
Karma: 8
Posts: 291
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The code is too massive to show. I'll write up a simpler model when get the chance. Im getting a headache all of a sudden. Straining too much at the monitor I guess.
Logged

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The serial print would yield 129. I'm assuming that's an address and not the value of deviceState which should either be 0 or 1.

Looks like it's not. Your code is correct, and nevertheless 129 is pretty low for an address
Logged

0
Offline Offline
Sr. Member
****
Karma: 8
Posts: 291
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yeah, I made a quick test to see... everything turned out as expected. I'll have track down the issue.
Code:
#include <iostream>

class Device
{
public:
    bool deviceState;
    Device *dependent_device;
    Device(Device *in_device) {
        dependent_device = in_device;
        deviceState = true;
        
    }
    void printThis() {
        bool temp = dependent_device->deviceState;
        printf("state: %d \n", temp);
    }
};


int main()
{

    Device *a = new Device(nullptr);
    Device *b = new Device(a);
    b->printThis();
    
    
    
    return 0;
}
« Last Edit: March 24, 2013, 09:10:47 pm by mistergreen » Logged

Offline Offline
Faraday Member
**
Karma: 62
Posts: 3077
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I still don't see an actual problem with your original code.  If you get '129' and you were expecting something else,
it is possible that the variable that you expected to be 'true' or 'false'   wasn't initialized anywhere.
Logged

0
Offline Offline
Sr. Member
****
Karma: 8
Posts: 291
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I found the issue. I'm not sure why but declaring it in the base class AND the inherited class made it act funny. I just left it declared in the based class only.
Logged

Pages: [1]   Go Up
Jump to: