473,320 Members | 1,879 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,320 software developers and data experts.

[BEGINNER] Some questions

I need help with the following questions:
a) if a class has virtual functions, is it better to make the
destructor virtual
b) can a friend function be inherited
c) if a function argument is const, is it better to pass non-const
values
The following questions don't make sense to me, but they are asked in
an interview
d) if B is a subclass of A, can an array of A's be used in the place of
an array of B
e) is the first catch statement is used or the best match is used in a
catch block
f) can reference to an object modified to point to another object

Thanks

Jul 23 '05 #1
6 1131
"Last Timer" <da********@yahoo.com> wrote...
I need help with the following questions:
a) if a class has virtual functions, is it better to make the
destructor virtual
It definitely won't hurt [much]. It can help in certain situations.
b) can a friend function be inherited
Friendship is not inhertied nor can it be transferred.
c) if a function argument is const, is it better to pass non-const
values
I am not sure I understand the question.
The following questions don't make sense to me, but they are asked in
an interview
d) if B is a subclass of A, can an array of A's be used in the place of
an array of B
The term "subclass" is definitely not C++. IIUIC here, B is derived
from A. In that case, no, an array of A cannot be used in place of
an array of B.
e) is the first catch statement is used or the best match is used in a
catch block
The best match.
f) can reference to an object modified to point to another object


No, it cannot. The [hypothetical] act of "pointing to another object"
is called "reseating a reference", and in C++ it cannot be done.

V
Jul 23 '05 #2
Victor Bazarov schrieb:
e) is the first catch statement is used or the best match is used in a
catch block

The best match.


That's not true. The first statement that maches is used.
Jul 23 '05 #3
"Michael Etscheid" <th***********@gmail.com> wrote...
Victor Bazarov schrieb:
e) is the first catch statement is used or the best match is used in a
catch block

The best match.


That's not true. The first statement that maches is used.


So, if the "first catch statement" doesn't match, it is not used, right?
Read the question again.
Jul 23 '05 #4

"Michael Etscheid" <th***********@gmail.com> wrote in message
news:d0*************@news.t-online.com...
Victor Bazarov schrieb:
e) is the first catch statement is used or the best match is used in a
catch block

The best match.


That's not true. The first statement that maches is used.


Well, IMHO the first one that matches is implicitly the best one as the
standard mandates that the handlers are tried in order of appearance.

Chris
Jul 23 '05 #5
Victor Bazarov wrote:
"Michael Etscheid" <th***********@gmail.com> wrote...
Victor Bazarov schrieb:
e) is the first catch statement is used or the best match is used in a
catch block
The best match.


That's not true. The first statement that maches is used.

So, if the "first catch statement" doesn't match, it is not used, right?
Read the question again.


Without the testosterone <g>:

struct base {};
struct derived : base {}

void f()
{
try {
throw derived();
}
catch(int)
{
cout << "We never get here.\n";
}
catch(const base&)
{
cout << "We get here, 'cause this is the first match.\n"
}
catch(const derived&)
{
cout << "We don't get here, even though "
"this is the 'best' match.\n"
}

Naturally, this explains why catch(...) should only be used as the last
catch clause in a series.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #6
> if a class has virtual functions, is it better to make the destructor
virtual

Absolutely. Consider the following:

class base
{
public:
base() {}
~base() {}
};

class derived : public base
{
some_big_class* p;

public:
derived() { p = new some_big_class(); }
~derived() { delete p; }
};

main()
{
base* myobj = new derived();
// ...
delete myobj;
}
The destructor will never be called, and your program will leave blobs
of garbage behind it. If you make the destructor virtual, the 'delete
p;' line will be called and your memory leak will be fixed. (You can
convince yourself of this by adding a line of output in derived's
constructor.)

if B is a subclass of A, can an array of A's be used in the place of

an array of B

This is asking if the following.

Since B derives from A, an object of type B can be used any place an
object of type A is expected, e.g.:
void foo(A* somearg);
// ...
B someobj;
foo(&someobj);
This is okay. But should an object of type B[] be substituted for a
type A[]? So should the following work?
void foo(A somearg[]);
// ...
B somearray[100];
foo(somearry);
My understanding (and Victor's answer) is no. From an implementation
perspective, the sizes of A and B are different. Thus when foo looks
for somearg[1], it won't be where it thinks it is because that will be
the end of somearray[0]. Graphically:
| somearg[0] | somearg[1] | .... | somearg[n-1]
| somearray[0] | somearray[1] | ... |
somearray[n-1]

More theoretically, imagine somearg[] as a collection of base objects.
Thus foo should be able to add something to that collection (and could
if the program was using vectors or another containter class instead of
arays). But foo thinks they are base objects, so adds a new base
object. But now somearray[], which is supposed to be a collection of
derived objects, now isn't because it has a base object in it! Oops!
Despite what I said before, the following code compiles and crashes at
runtime under VS.Net 7. Is this correct behavior on the part of the
compiler?

int gid;

class base
{
int id;
public:
virtual int ID() { return id; }
base() { id = gid++; }
virtual ~base() {}
};

class derived : public base
{
int did;
public:
int ID() { return did; }
derived() {base(); did = 1000;}
~derived() { }
};
void foo(base a[])
{
for(int i = 0 ; i<100 ; ++i)
cout << a[i].ID() << endl;
}

int main()
{
derived d[100];
foo(d);
}

Jul 23 '05 #7

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

Similar topics

0
by: Atip Asvanund | last post by:
Dear sirs, I am trying to learn how to use Boehm's garbage collector: http://www.hpl.hp.com/personal/Hans_Boehm/gc/ on a Linux machine. I am a beginner, and I find its documentation inadequate....
7
by: Rensjuh | last post by:
Hello, does someone have / know a good C++ tutorial for beginnners? I would prefer Dutch, but English is also fine. Hoi, heeft / kent iemand nog een goede C++ tutorial voor beginners? Het liefste...
27
by: MHoffman | last post by:
I am just learning to program, and hoping someone can help me with the following: for a simple calculator, a string is entered into a text box ... how do I prevent the user from entering a text...
1
by: Robert J. Bonn | last post by:
I'm trying to set up a contact list in MS Access 97. I've looked through a reference book and the program's help screens, but the light bulb isn't quite coming on for me. If one of you could take...
22
by: ddg_linux | last post by:
I have been reading about and doing a lot of php code examples from books but now I find myself wanting to do something practical with some of the skills that I have learned. I am a beginner php...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.