C++ class pointer usage issues. #145078
-
Error messages:
C++ code:#include <memory>
#include <stdexcept>
#include <iostream>
template <typename T>
class automatic_ptr {
private:
std::shared_ptr<T>pointer;
public:
// Setup class constructor and deconstructor
automatic_ptr() {};
~automatic_ptr() {};
/**
* @param ptr
* @brief This function takes the pointer ptr and makes the class pointer point to it
**/
explicit automatic_ptr(std::make_shared<T>ptr) : pointer (ptr) {}
// Add operators
T& operator -> () const {
return pointer.get();
}
T& operator * () const {
return *pointer;
}
};
int main() {
automatic_ptr<int>* autoptr(std::shared_ptr<int>(0));
*autoptr = 5;
std::cout << *autoptr;
} Guidelines
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Up vote if you hate C++ to. |
Beta Was this translation helpful? Give feedback.
-
So first of all it's C++ that hates you not the other way around. You just butchered C++, the code you made makes no sense, and the error messages are pretty straight forward to understand. You clearly lack foundamental knowledge. Other than that, what exactly are you trying to accomplish with this class? From what I think you are trying to do, is a class wrapper for a shared pointer that just overloads the operators Let's first get into the stupid errors:
std::shared_ptr<int> p(new int); The main difference is that this way it requires two memory allocations: one for the managed object Both of these ways return a pointer with allocated memory that you would then pass to the constructor, not the other way around like you did.
automatic_ptr<int>* autoptr = new automatic_ptr<int>(std::make_shared<int>());
here's the revised code, but still note all this crap is useless unless you tell me what the purpose of this class would be other than doing what already can be done with normal shared_ptr usage #include <memory>
#include <stdexcept>
#include <iostream>
// An struct to use the "->" operator;
struct Example {
int value;
};
template <typename T> class automatic_ptr {
private:
std::shared_ptr<T> pointer;
public:
/**
* @brief Class constructor, takes a shared pointer and makes the class pointer attribute point to it
* @param ptr must take a std::make_shared<T> argument
**/
explicit automatic_ptr(std::shared_ptr<T> ptr) :
pointer(ptr) {
}
// Destructor (not strictly needed for shared_ptr)
~automatic_ptr() {};
// Overloaded operators
T *operator->() const {
return pointer.get();
}
T &operator*() const {
return *pointer;
}
};
int main() {
// ---------------------------- Raw Pointer To Your automatic_ptr object -------------------------------------
// Create a raw pointer to automatic_ptr object and manually allocate memory for it
automatic_ptr<Example> *rawautoptr1 = new automatic_ptr<Example>(std::make_shared<Example>());
automatic_ptr<int> *rawautoptr2 = new automatic_ptr<int>(std::make_shared<int>());
// Assign a value to the raw pointer of automatic_ptr object
(*rawautoptr1)->value = 420;
**rawautoptr2 = 4;
// Print the values of the raw pointer of automatic_ptr object
std::cout << "automatic_ptr<Example>*: " << (*rawautoptr1)->value << std::endl;
std::cout << "automatic_ptr<int>*: " << **rawautoptr2 << std::endl;
// Clean up the allocated memory, shared pointers would have already taken care of this crap...
delete rawautoptr1;
delete rawautoptr2;
// ---------------------------- Your automatic_ptr object -------------------------------------
// Create an automatic_ptr object wrapping a shared pointer to a struct and int
automatic_ptr<Example> autoptr1(std::make_shared<Example>());
automatic_ptr<int> autoptr2(std::make_shared<int>());
// Assign a value to the automatic_ptr object
autoptr1->value = 42;
*autoptr2 = 5;
// Print the values of the automatic_ptr object
std::cout << "automatic_ptr<Example>: " << autoptr1->value << std::endl;
std::cout << "automatic_ptr<int>: " << *autoptr2 << std::endl;
// ---------------------------- Normal shared_ptr Usage-------------------------------------
// Create a shared_ptr pointing to a struct and int
std::shared_ptr<Example> sharedprt2 = std::make_shared<Example>();
std::shared_ptr<int> sharedptr1 = std::make_shared<int>();
// Assign a value to the shared pointer object
sharedprt2->value = 69;
*sharedptr1 = 7;
// Print the value of shared pointer object
std::cout << "shared_ptr<Example>: " << sharedprt2->value << std::endl;
std::cout << "shared_ptr<int>: " << *sharedptr1 << std::endl;
} |
Beta Was this translation helpful? Give feedback.
So first of all it's C++ that hates you not the other way around. You just butchered C++, the code you made makes no sense, and the error messages are pretty straight forward to understand. You clearly lack foundamental knowledge.
Other than that, what exactly are you trying to accomplish with this class?
From what I think you are trying to do, is a class wrapper for a shared pointer that just overloads the operators
->
and*
.Let's first get into the stupid errors:
make_shared<T>
needs to be called first to create and allocate memory for ashared_pointer<T>
. The alternative would have ben to allocate it manually with thenew
keyword, like this: