Doubt on Constructors

E

edu.mvk

Hi,

in the below statement :

class A {
int *i;
A(){

};

main()
{
A* a=new A();
}

if suppose the constructor A() will create a socket and while doing
that it fails and we didnt catch the exception then will the variable
"a" gets the memory..??
 
J

Jakob Bieling

edu.mvk said:
class A {

public:
int *i;
A(){

}

Do not type in your news reader. Copy and paste from the source you
compiled (or tried to). People might want to try to compile your code
without having to fix dozens of errors first.
};

main()

int main ()
{
A* a=new A();

delete a;
}

if suppose the constructor A() will create a socket and while doing
that it fails and we didnt catch the exception then will the variable
"a" gets the memory..??

When an exception is thrown, that you do not catch, any code after
the exception was thrown will not be executed. That is, 'a' will never
point to anything meaningful, if the ctor of A throws.

hth
 
D

Daniel T.

"edu.mvk said:
Hi,

in the below statement :

class A {
int *i;
A(){

};

main()
{
A* a=new A();
}

if suppose the constructor A() will create a socket and while doing
that it fails and we didnt catch the exception then will the variable
"a" gets the memory..??

Nothing will get the memory. The memory will be given back to the
"system". Unless, of course, someone re-implemented operator new and did
it incorrectly.
 
R

Ron Natalie

Jakob said:
Do not type in your news reader. Copy and paste from the source you
compiled (or tried to). People might want to try to compile your code
without having to fix dozens of errors first.
Further, they will point out all the errors in your code without
answering your real question. By the way, the class A() is
can't be instantiated via new due the private default constructor.
 
E

edu.mvk

Daniel said:
Nothing will get the memory. The memory will be given back to the
"system". Unless, of course, someone re-implemented operator new and did
it incorrectly.

Thx for ur answer Daniel,

i have a small doubt..

whats the situation if i didnt throw any exception from that
constructor and the socket creation has failed because of some
reason..?? ( we didnt use the exception handling for that failure )

will the object be created or not ?
 
H

Howard

edu.mvk said:
Thx for ur answer Daniel,

Please use complete words, like "Thanks for your...". This isn't a
text-messager, or instant messenger. A lot of people here are not native
English speakers, and have a hard enough time with English, without having
to also interpret "IM-speak".
i have a small doubt..

whats the situation if i didnt throw any exception from that
constructor and the socket creation has failed because of some
reason..?? ( we didnt use the exception handling for that failure )

If the creation of the socket fails, and no exception is thrown (either by
you or by the call to create the socket itself), then your object will be
created normally. But obviously you'll have problems later trying to use
the socket, won't you? :)

Of course, we don't know anything about how you're attempting to create the
socket or what it does when it fails to create properly, so we can't tell
you what would happen if it "failed because of some reason".

-Howard
 
D

Daniel T.

"edu.mvk said:
Thx for ur answer Daniel,

i have a small doubt..

whats the situation if i didnt throw any exception from that
constructor and the socket creation has failed because of some
reason..?? ( we didnt use the exception handling for that failure )

will the object be created or not ?

Yes it will.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top