Rookie c++ question, but Ive spent the last 5 years doing Java, where
everytime I created an object I used new. In c++ I can create my objects
without and its confusing me just a little.
I have a class called polynomial. Its a nothing little class right now,
with just int variables, a basic container class. Im using it as I go
through some tutorials, but in this particular tutorial its telling me to do
polynomial *first = new polynomial();
but before I found this site I was just doing
polynomial first;
Im also struggling through pointers. I understand the basics, but fail to
see the advantage using them with my objects so quickly. Is one way better
than the other?
Thanks
Jul 22 '05
24 2862
>Such an object has it's lifetime managed by the programmer. What's your point?
I interpreted your statement to mean that one should *only* use new for the
purposes of managing an objects lifetime.
I suggest that another purpose for using new is to defer the decision as to
which type of object to create to run-time.
It is true that the object will have its lifetime managed by the programmer but
the responsibility for lifetime management might be a consequence rather than a
goal.
void DoSomething(con st string& typeIndicator)
{
Base* base = SomeFactoryFunc tion(typeIndica tor);
delete base;
}
In this case the lifetime of the object presumably created with new is
identical to that of a variable with automatic storage duration. The need to
explicitly manage the lifetime of the object seems to be a consequence that
resulted from the need to delay the decision as to which kind of object to
create until run-time.
My only point is that lifetime management in one case might be exactly what is
desired whereas in another case it might just be a "side-effect".
"DaKoadMunk y" <da*********@ao l.com> wrote in message
news:20******** *************** ****@mb-m04.aol.com... Such an object has it's lifetime managed by the programmer. What's your point?
I interpreted your statement to mean that one should *only* use new for the purposes of managing an objects lifetime.
I suggest that another purpose for using new is to defer the decision as to which type of object to create to run-time.
It is true that the object will have its lifetime managed by the programmer but the responsibility for lifetime management might be a consequence rather than a goal.
void DoSomething(con st string& typeIndicator) { Base* base = SomeFactoryFunc tion(typeIndica tor);
delete base; }
In this case the lifetime of the object presumably created with new is identical to that of a variable with automatic storage duration. The need to explicitly manage the lifetime of the object seems to be a consequence that resulted from the need to delay the decision as to which kind of object to create until run-time.
My only point is that lifetime management in one case might be exactly what is desired whereas in another case it might just be a "side-effect".
I'm afraid you've lost me. I need a practical example of how a pointer could
be used without making it necessary to manage an object's lifetime. Dynamic
typing does make it necessary so as an example it isn't good enough. da*********@aol .com (DaKoadMunky) wrote in message news:<20******* *************** *****@mb-m11.aol.com>... Only use a pointer when you need to control the lifetime of an object. What if the type of object to be created is not known until run-time?
True, you need a pointer or reference there. A reference is often
a solution if you need either one of N possible globals.
i.e.
base& get1() { static der1 r; return r; }
base& get2() { static der2 r; return r; }
base& the_chosen = runtime_cond() ? get1() : get2();
What if the number of objects to be created is not known until run-time?
std::vector<typ e>, std::list<type> , std::deque<type >
At the lowest level I believe the new operator would be required.
It's often used, including array new[], and placement new(here). But
the first example shows there are alternatives. And even where you
use new, it's bet used indirectly - e.g. as part of a std:: class.
BTW, even if you use new directly, avoid the need for a matching
delete. Use a smart pointer instead, e.g. boost::scoped_p tr.
Regards,
Michiel Salters
In message <41************ ***********@new s.optusnet.com. au>, Jason Heyes
<ja********@opt usnet.com.au> writes "DaKoadMunky " <da*********@ao l.com> wrote in message news:20******* *************** *****@mb-m04.aol.com... >Such an object has it's lifetime managed by the programmer. What's your point? I interpreted your statement to mean that one should *only* use new for the purposes of managing an objects lifetime.
I suggest that another purpose for using new is to defer the decision as to which type of object to create to run-time.
It is true that the object will have its lifetime managed by the programmer but the responsibility for lifetime management might be a consequence rather than a goal.
void DoSomething(con st string& typeIndicator) { Base* base = SomeFactoryFunc tion(typeIndica tor);
delete base; }
In this case the lifetime of the object presumably created with new is identical to that of a variable with automatic storage duration. The need to explicitly manage the lifetime of the object seems to be a consequence that resulted from the need to delay the decision as to which kind of object to create until run-time.
My only point is that lifetime management in one case might be exactly what is desired whereas in another case it might just be a "side-effect".
I'm afraid you've lost me. I need a practical example of how a pointer could be used without making it necessary to manage an object's lifetime.
Easy. Make it point to something that's not on the heap.
class Base { /*...*/};
class D1: public Base{/*...*/};
class D2: public Base{/*...*/};
/* etc. */
void DoSomething(int typeIndicator)
{
D1 d1;
D2 d2;
/* etc. */
Base * p=0;
switch (typeIndicator)
{
case 1: p = &d1; break;
case 2: p = &d2; break;
/*...*/
}
assert(p);
p->doSomething( );
// note the absence of "delete p;"
}
Dynamic typing does make it necessary so as an example it isn't good enough.
--
Richard Herring BTW, even if you use new directly, avoid the need for a matching delete. Use a smart pointer instead, e.g. boost::scoped_p tr.
Regards, Michiel Salters
Or, roll your own smart pointer class, which is a trivial thing to do,
with the benefit of totally controlling what's in the smart ptr class
(behaivour, etc), and without the hassle of importing another library
into the already muddled mix of libraies one have to deal with.
RCS This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Yi |
last post by:
Hi,
I am a scientist and new to .NET programming. On my PC, I am using Windows
2000 professional with IIS, SQL Server 2000 (standard, personal), Visual
Basic .net (2003, standard). By following the demo code, I can generate the
web form to access the Northwind data in SQL server. When I tried to use
Server Explorer to add new Data Connection to Northwind, the “test
connection” always work, but click OK always give an error message:...
|
by: dpr |
last post by:
I have come accross a piece of C++ code with the construct:
MyClass *c = new class MyClass();
Is there a difference between this and:
MyClass *c = new MyClass();
?
|
by: Leslaw Bieniasz |
last post by:
Cracow, 28.10.2004
Hello,
I have a program that intensively allocates and deletes lots
of relatively small objects, using "new" operator. The objects
themselves are composed of smaller objects, again allocated using "new".
From my tests I deduce that a considerable part of the computational
time is spent on the memory allocation, which makes the program
substantially slower compared to the equivalent code written using
|
by: Ben R. |
last post by:
I'm curious about the differeng behavior of the "new" keyword when dealing
with value versus object types. If I'm correct, when I do:
dim x as integer
There's no need for "new" to be called since this "value" type is all set to
go. Is this because value types are stored on the stack so the memory
initialization is already taken care of?
Now, when dealing with an object type, it seems you do need to use the "new"
|
by: Ron Cook |
last post by:
So I'll type something like:
Dim cmd As New SqlCommand()(
But for some things, it doesn't like "As New" and wants me to type like:
Dim nod As employeeNode
When do I know when to use "As New" as opposed to not? Intellisense always
sets me straight, but I'd like to understand better.
| |
by: Medvedev |
last post by:
i see serveral source codes , and i found they almost only use "new"
and "delete" keywords to make they object.
Why should i do that , and as i know the object is going to be destroy
by itself at the end of the app
for example:
class test
{
public:
int x;
|
by: jonceramic |
last post by:
Hi All,
My apologies for asking something that I'm sure has been answered
for. But, my google searching can't find a proper set of keywords.
I would like to add "New..." or "other..." or "Show All" to my combo
boxes.
I'm tired of having to put buttons beside them for "special options".
|
by: Jordi |
last post by:
I'm getting the following error:
Software error:
Can't locate object method "new" via package "A::B" at /path/file.cgi line 5.
My code is basically this:
#!/usr/bin/perl -w
use strict;
use warnings;
use A::B;
my $test = new A::B;
|
by: tvnaidu |
last post by:
I compiled tinyxml files (.cpp files) and when I am linking to create final exe in Linux, I am getting lot of errors saying "undefiend reference" to "operator new" and "delete", any idea?.
Main.cpp:377: undefined reference to `operator delete(void*)'
tinyXML/include/tinystr.h:259: undefined reference to `operator delete(void*)'
../common/tinyXML/include/tinyxml.h:1401: undefined reference to `operator delete(void*)'...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |