473,383 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

is it a good idea to assign object to pointer without initialising it to NULL.

hi all,
is it a good idea to assign object to pointers without initialising it
to NULL?
suppose if i have a class TEST

class ELEMENT
{
private:
int a;
string s1;
public:
func(a);
AddChild()
{
cout<<"some value";
}
};

i have object of TEST as shown below:
ELEMENT *inventors=biblio->AddChild("Invs");
ELEMENT *t2;
is it better to initialize while declaring itself?
i mean,if i say :

ELEMENT *t2= inventors->AddChild("Inv");// is this safe enough?

or do i need to go with usual conventions like:
ELEMENT *t2=NULL;
t2= inventors->AddChild("Inv");

so please help me which one is better and safe?

if not please explain me what could go wrong with

ELEMENT *t2= inventors->AddChild("Inv");

thanks & regards
Nagaraj Hayyal

Sep 19 '05 #1
8 1751
nrhayyal wrote:

i have object of TEST as shown below:
ELEMENT *inventors=biblio->AddChild("Invs");
ELEMENT *t2;
is it better to initialize while declaring itself?
i mean,if i say :

ELEMENT *t2= inventors->AddChild("Inv");// is this safe enough?

or do i need to go with usual conventions like:
ELEMENT *t2=NULL;
t2= inventors->AddChild("Inv");

so please help me which one is better and safe?


There is no difference at all.
It all depends on the return value of AddChild if the pointer
is valid or not. In both cases.

'Prefer initialization above assignment'

--
Karl Heinz Buchegger
kb******@gascad.at
Sep 19 '05 #2
thanks for ur reply Karl ,
is there any reason for 'Preferring initialization above assignment'.?
just curious to know about it.

Sep 21 '05 #3
nrhayyal wrote:

thanks for ur reply Karl ,
is there any reason for 'Preferring initialization above assignment'.?
just curious to know about it.


In the worst case there is no difference between initialization and assignment.
Example:

int j = 5;

------

int j;
j = 5;

I would expect both codes to produce identical code.

But: There may be a difference if the variable you define is of
some class type:

std::string Name( "Compiler" );

------

std::string Name;
Name = "Compiler";

Those 2 code snippets do different things. The first snippet uses
a special constructor to create the Name object with a specific value.
The second code snippet however, creates the Name object and initialises
it to an empty string. The following assignment then discards that empty
string and assigns it a new value.
So while in the first case, the whole process is a one-step procedure (create
object with a specific value) it becomes a two-step procedure in the second
case (create empty object, alter empty object).

And then there is of course the case, where assignment simply doesn't work, eg.
because the 'variable' you define is a constant. A constant can only be initialized
but not assigned, hence initialization is the only thing that will work:

const int j = 5; // works

-------

const int j;
j = 5; // does not work. j is const, hence the compiler hinders you
// to change its value.

So all in all: There is no advantage in assignment in all those cases, so why use it?
Get used to use initialization and you have less problems in the long run.

--
Karl Heinz Buchegger
kb******@gascad.at
Sep 21 '05 #4
> nrhayyalwrote:
i have object of TEST as shown below:
ELEMENT *inventors=biblio->AddChild("Invs");
ELEMENT *t2;
is it better to initialize while declaring itself?
i mean,if i say :

ELEMENT *t2= inventors->AddChild("Inv");// is this safe enough?
What is biblio, is it a object? If it's ,you are doing wrong,
First you have to new up the object, then you can call
any function with in that class, using pointer,
Eg:
ELEMENT *inventors=new ELEMENT();
inventors->AddChild("Inv");

Thanks,
Bangalore


Sep 21 '05 #5
On Wed, 21 Sep 2005 10:47:55 +0200, Karl Heinz Buchegger <kb******@gascad.at>
wrote:
So all in all: There is no advantage in assignment in all those cases, so why use it?
Get used to use initialization and you have less problems in the long run.


Right. Also:

class A
{
public:
A(int) {} // Constructor taking an int
A(const A&) {} // Copy constructor
};

int main()
{
A a0(1);
A a1 = 2;
return 0;
}
When instantiating a0, the constructor taking an int is used in all cases.

When initializing a1, however, the semantic is A(A(2)), which technically
constructs a temporary object using the construtor taking an int, then passing
it to the copy constructor of a1.

-dr
Sep 21 '05 #6

Dave Rahardja wrote:
On Wed, 21 Sep 2005 10:47:55 +0200, Karl Heinz Buchegger <kb******@gascad.at>
wrote:
So all in all: There is no advantage in assignment in all those cases, so why use it?
Get used to use initialization and you have less problems in the long run.


Right. Also:

class A
{
public:
A(int) {} // Constructor taking an int
A(const A&) {} // Copy constructor
};

int main()
{
A a0(1);
A a1 = 2;
return 0;
}
When instantiating a0, the constructor taking an int is used in all cases.

When initializing a1, however, the semantic is A(A(2)), which technically
constructs a temporary object using the construtor taking an int, then passing
it to the copy constructor of a1.

Hi,

Are you sure about that? Please, just test your example.

Best regards,
Bogdan Sintoma

Sep 22 '05 #7
Bogdan Sintoma wrote:

Dave Rahardja wrote:

Right. Also:

class A
{
public:
A(int) {} // Constructor taking an int
A(const A&) {} // Copy constructor
};

int main()
{
A a0(1);
A a1 = 2;
return 0;
}
When instantiating a0, the constructor taking an int is used in all cases.

When initializing a1, however, the semantic is A(A(2)), which technically
constructs a temporary object using the construtor taking an int, then passing
it to the copy constructor of a1.

Hi,

Are you sure about that? Please, just test your example.


Yes, he sure about that. And yes, he is right.
Your tests may show a difference, since the compiler is allowed to
optimize that temporary away. Nevertheless the semantics is indeed A(A(2))
which can easily be seen by making the copy constructor unaccessible. In this
case the compiler must refuse to compile this code, since it cannot use the
copy constructor (even if that copy constructor gets optimized away).

--
Karl Heinz Buchegger
kb******@gascad.at
Sep 22 '05 #8

Karl Heinz Buchegger wrote:
Bogdan Sintoma wrote:

Dave Rahardja wrote:

Right. Also:

class A
{
public:
A(int) {} // Constructor taking an int
A(const A&) {} // Copy constructor
};

int main()
{
A a0(1);
A a1 = 2;
return 0;
}
When instantiating a0, the constructor taking an int is used in all cases.

When initializing a1, however, the semantic is A(A(2)), which technically
constructs a temporary object using the construtor taking an int, then passing
it to the copy constructor of a1.

Hi,

Are you sure about that? Please, just test your example.


Yes, he sure about that. And yes, he is right.
Your tests may show a difference, since the compiler is allowed to
optimize that temporary away. Nevertheless the semantics is indeed A(A(2))
which can easily be seen by making the copy constructor unaccessible. In this
case the compiler must refuse to compile this code, since it cannot use the
copy constructor (even if that copy constructor gets optimized away).

--

And yes, you are right. My mistake.

Best regards,
Bogdan Sintoma

Sep 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
by: Christian Seberino | last post by:
How does Ruby compare to Python?? How good is DESIGN of Ruby compared to Python? Python's design is godly. I'm wondering if Ruby's is godly too. I've heard it has solid OOP design but then...
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
16
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have...
18
by: hzmonte | last post by:
typedef int t_compare_func(const void *, const void *); struct node *tree_search(struct node *root, const void *keyy, t_compare_func *comp) { struct node *cur_item; int result; if (root ==...
13
by: Max | last post by:
Hi There! I'm having a mysterious error right after I login using Forms Authentication in my ASP.NET app. Below is the error... Exception Details: System.NullReferenceException: Object...
69
by: fieldfallow | last post by:
Hello all, Before stating my question, I should mention that I'm fairly new to C. Now, I attempted a small demo that prints out the values of C's numeric types, both uninitialised and after...
5
by: Johs32 | last post by:
I have a struct "my_struct" and a function that as argument takes a pointer to this struct: struct my_struct{ struct my_struct *new; }; void my_func(struct my_struct *new); I have read...
75
by: Amkcoder | last post by:
http://amkcoder.fileave.com/L_BitWise.zip http://amkcoder.fileave.com/L_ptr2.zip
50
by: arunajob | last post by:
Hi all, If I have a piece of code something like this void main(void) { char * p1="abcdefghijklmn"; ............................................. }
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.