Reputation: 827
I have a pretty weak understanding of pointers in the very first place, but have had little luck finding out exactly why you need them in class definitions.
I know that pointers are a type of variable that point to the memory address of another variable. That's about where my real understanding begins and ends.
However, I can't possibly see their use in the beginning of this derived class:
class Circle : public Shape {
private: int* radius;
....
....
}
Why would radius be a pointer? What purpose does this serve?
Upvotes: 0
Views: 193
Reputation: 3397
Pointers' use cases are based on fact that two objects whose pointers are referring the same memory location can communicate via that memory.
Three main purposes are:
BigFatObject
as a parameter, then you declare this parameter to have type BigFatObject*
. That way you reduce stack usage from sizeof(BigFatObject)
(which can be huge) to sizeof(void*)
(which is typically only 4/8 bytes)For example:
class SharedObject
{
public:
int getValue() { return value; }
void setValue(int value) { this->value = value; }
private:
int value;
};
class CommunicatesThroughSharedObject
{
public:
CommunicatesThroughSharedObject(SharedObject *shared)
{
this->shared = shared;
}
private:
SharedObject *shared;
};
SharedObject shared;
CommunicatesThroughSharedObject c1(&shared), c2(&shared);
Now you can get c1
and c2
communicating via calling their shared
variable's getValue
and setValue
methods
Upvotes: 3
Reputation: 15334
You would typically use a pointer to refer to a resource that Class does not own, perhaps it is shared with other circles. Pointers can also be useful for polymorphism. The pointer could point at a base class and the actual type is not decided until run time. All this could be achieved with more safety using references but pointers have the advantage of being nullable so could be used if the resource might not exist at the point that the Circle is constructed.
This does not make much sense for a radius but maybe for a logger:
#include <iostream>
#include <string>
class Logger {
public:
virtual ~Logger() {}
virtual void log(const std::string& message) = 0;
};
class FileLogger : public Logger {
public:
void log(const std::string& message) {/*...*/ }
};
class TestLogger : public Logger {
public:
void log(const std::string& message) { std::cout << message << std::endl;}
};
class Shape {
//...
};
class Circle : public Shape {
private:
int radius;
Logger* logger;
//...
public:
Circle() : radius(1), logger(nullptr) {}
void setLogger(Logger* in) {logger = in;}
void doSomething() {
//...
if (logger != nullptr)
logger->log("Did something");
}
};
int main() {
Circle circle;
circle.doSomething();
TestLogger logger;
circle.setLogger(&logger);
circle.doSomething();
return 0;
}
Upvotes: 0
Reputation: 10199
Pointers allow you to take advantage of a lot of nifty language features, polymorphism and dynamic memory allocation being the most important of the top of my head. Some other techniques such as the pImpl
Idiom also use pointers.
In the example above, the declaration of radius
as a pointer seems counter-intuitive (indeed, one can argue that it is not necessary: it does not provide any real benefits and yet complicates things for the programmer).
However, there are cases where pointers-as-members are very useful. Consider the following case where a class representing a Person:
class Person
{
char* _name;
public:
Person(const char* name);
};
Now, declaring the member name
as a pointer allows you to allocate memory later, therefore you can allocate only as much memory as needed via the ctor
. You can used arrays to do the same, but you'd end up wasting memory.
As a side note, the code for Person
class is definitely not the proper C++ way of doing things. Using a std::string
would be the way to go.
Upvotes: 1