Home > Software engineering >  Struggling with pointers to functions and references
Struggling with pointers to functions and references

Time:01-20

I am working through this problem I found on Git to brush up on some skills. Using friend is prohibited. C styling should be used compared to C.

Essentially, I cannot call the identify() function that belongs to the Brain member variable in my Human class. It just will not let me access it. If you can code this up, and explain where I am going wrong, that would be great.

Create a Brain class, with whatever you think befits a brain. It will have an Identify() function that returns a string containing the brain's address in memory, in hex format, prefixed by 0x.

Then, make a Human class, that has a constant Brain attribute with the same lifetime. It has an identify() function, that just calls the identity() function of its Brain and returns its result.

Now, make it so this code compiles and displays two identical addresses:

int main(){
    Human bob;
    std::cout << bob.identify() << "\n";
    std::cout << bob.getBrain().identify() << "\n";
}

Here is what I have so far:

#pragma once
#include "Brain.h"

class Human
{
    const Brain humanBrain;

public:
    Human();
    std::string identify();
};
#include "Human.h"
#include <iostream>
#include <string>
#include <sstream>

Human::Human()
{
    this->humanBrain = new Brain;
}

std::string Human::identify()
{
    Brain b = this->humanBrain.identify(); // This is essentially what I am trying to call--and I can't access it.
    const Brain * ptr = humanBrain;
    std::ostringstream test;
    test << ptr;
    return test.str();
}
#pragma once
#include <string>
#include <iostream>

class Brain
{
    int age;
    std::string gender;
    void* ptr;
public:
    Brain();
    //std::string getBrain();
    const std::string identify();
    void setPtr(void* p);
};
#include "Brain.h"
#include <iostream>
#include <sstream>

Brain::Brain()
{
    age = 10;
    gender = "male";
}

const std::string Brain::identify()
{
    //const Brain* bPtr = &this;
    const Brain* bPtr = this;
    ptr = this;
    std::ostringstream test;
    test << &bPtr;
    std::string output = "Brain Identify: 0x"   test.str();

    return output;
}

CodePudding user response:

Your Human::humanBrain member is declared as type const Brain, which is correct per the instructions, however your Brain::identify() method is not qualified as const, so you can't call it on any const Brain object. This is the root of the problem that you are having trouble with.

In addition, there are many other problems with your code, as well:

  • Human::humanBrain is not a pointer, so using new to construct it is wrong. And, you don't need a pointer to get the address of a variable anyway. Nor do you actually need a pointer to the member at all in this project.

  • Human lacks a getBrain() method, so bob.getBrain() in main() will not compile, per the instructions.

  • Human::identify() is calling humanBrain.identify(), which returns a std::string as it should, but is then assigning that string to a local Brain variable, which is wrong (not to mention, you are not even using that variable for anything afterwards). The instructions clearly state that Human::identity() should simply call Brain::identify() and return its result, but you are not doing that.

  • Brain::identify() is printing the address of its local variable bPtr rather than printing the address of the Brain object that identify() is begin called on, per the instructions.

With all of that said, try something more like this instead:

Human.h

#pragma once
#include "Brain.h"
#include <string>

class Human
{
    const Brain humanBrain;

public:
    Human() = default;
    std::string identify() const;
    const Brain& getBrain() const;
};

Human.cpp

#include "Human.h"

std::string Human::identify() const
{
    return humanBrain.identity();
}

const Brain& Human::getBrain() const
{
    return humanBrain;
}

Brain.h

#pragma once
#include <string>

class Brain
{
    int age;
    std::string gender;
public:
    Brain();
    std::string identify() const;
};

Brain.cpp

#include "Brain.h"
#include <sstream>

Brain::Brain()
{
    age = 10;
    gender = "male";
}

std::string Brain::identify() const
{
    std::ostringstream test;
    test << "Brain Identify: 0x" << this;
    return test.str();
}
  •  Tags:  
  • c
  • Related