Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Can someone please help me understand what goes into allowing the use of "new"?  (Read 1249 times)

0 Members and 1 Guest are viewing this topic.

doonholdmichigin

  • Guest
hello, I apolgise for such a basic C++ question, but this is causing me to be unable to work efficiently. I come from a programming language background that constantly makes use of the keyword "new". Such as:

"Class _classVarName = New Class(constructor_param)"

and I somehow figured that this was inherent to all C++ classes. Apparently among the use of pointers and the like, C++ also forces YOU to make this possible. Otherwise you are left with default constructor uses which I am unfimiliar with such as:

"Class _classVarName; //Class is now declared and living at _classVarName;"

I have issues with this kind of declaration since I like to have important classes such as manager classes living inside of externally available var types. Or easily accessible singletons. Which are globals. Because I am lazy.

Could someone please explain what the process of allowing a operator= is?

I tried doing this but it didnt work:
"
                //placed inside the public section of a custom class
      const CustomClass& operator = (const CustomClass& x)
      {
         std::cout << "operator = " << std::endl;
         return *this;
      };
"

How come some libraries I use allow me to use the "new" keyword and some don't? What goes into allowing this storage of a class inside a pointer variable?


Gambit

  • Sr. Member
  • ****
  • Posts: 283
    • View Profile
New in C++ is not the same as new from C# or Java or the like. In C++ new allocates memory for your type and returns a pointer to the allocated memory which you must later deallocate or you will get memory leaks. Given that you don't know this already I would strongly advise against using new at all and instead look at using unique_ptr in c++11 and up.

DarkRoku12

  • Full Member
  • ***
  • Posts: 203
  • Lua coder.
    • View Profile
    • Email
I would strongly advise against using new at all and instead look at using unique_ptr in c++11 and up.

I would argue againts that, while i'm in favor of using smart pointers, the new operator is important to learn and have its places even on modern c++. But i don't want to start Another Raii discussion

You have a ton of c++ learning material.
New users tend to be attracted by Youtube to learn a programming language but i discourage it.
It's better to learn from books, well placed and answered question on C++ forums/topics. Like StackOverflow , cplusplus.com/forum.
If you like to see videos check out Channel9 from Microsoft.

And remember Google is your friend, search question using it.
I would like a spanish/latin community...
Problems building for Android? Look here