By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,898 Members | 2,012 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,898 IT Pros & Developers. It's quick & easy.

Doubt on Constructors

P: n/a
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..??

Jul 12 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
edu.mvk <ed*****@gmail.comwrote:
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
--
jb

(reply address in rot13, unscramble first)
Jul 12 '06 #2

P: n/a
In article <11**********************@h48g2000cwc.googlegroups .com>,
"edu.mvk" <ed*****@gmail.comwrote:
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.
Jul 12 '06 #3

P: n/a
Jakob Bieling wrote:
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.
Jul 12 '06 #4

P: n/a

Daniel T. wrote:
In article <11**********************@h48g2000cwc.googlegroups .com>,
"edu.mvk" <ed*****@gmail.comwrote:
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.
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 ?

Jul 12 '06 #5

P: n/a

"edu.mvk" <ed*****@gmail.comwrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...
>
Daniel T. wrote:
>In article <11**********************@h48g2000cwc.googlegroups .com>,
"edu.mvk" <ed*****@gmail.comwrote:
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.

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


Jul 12 '06 #6

P: n/a
In article <11**********************@p79g2000cwp.googlegroups .com>,
"edu.mvk" <ed*****@gmail.comwrote:
Daniel T. wrote:
In article <11**********************@h48g2000cwc.googlegroups .com>,
"edu.mvk" <ed*****@gmail.comwrote:
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.

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.
Jul 12 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.