Possible scopes of a destructor

P

__PPS__

Hello everybody,
in my exam in c++ I answered that destructors may be declared private,
public or protected.
The question was: "What possible scopes (public or private) can a
destructor have?"

I got 0 for this answer, they corrected me that destructor can only be
public.

When I showed the code with private destructor and friend functions
that create/destroy object they told me that I don't use it correctly,
that I don't call destructor etc etc....

So, what are the possible scopes destructors may have?

thans
 
M

Mike Wahler

__PPS__ said:
Hello everybody,
in my exam in c++ I answered that destructors may be declared private,
public or protected.
The question was: "What possible scopes (public or private) can a
destructor have?"

I got 0 for this answer, they corrected me that destructor can only be
public.

You're right, they're wrong. But I'd advise against using
other than a 'public' one unless You Really Really Know
What You're Doing, and have a very good reason.
When I showed the code with private destructor and friend functions
that create/destroy object they told me that I don't use it correctly,

I can't comment on that without seeing the code.
that I don't call destructor etc etc....

So, what are the possible scopes destructors may have?

Those you enumerated above.

-Mike
 
G

Greg Comeau

Hello everybody,
in my exam in c++ I answered that destructors may be declared private,
public or protected.
The question was: "What possible scopes (public or private) can a
destructor have?"

I got 0 for this answer, they corrected me that destructor can only be
public.

When I showed the code with private destructor and friend functions
that create/destroy object they told me that I don't use it correctly,
that I don't call destructor etc etc....

So, what are the possible scopes destructors may have?

Since the question asked about *scope* the correct answer
is "class scope".

However, it sounds like your question was about accessibility,
that is *access*. In that case, usually you don't want it to be,
but sure a dtor can be private. Of course, the case of not
using something properly is not always the same as if it's possible.
If you want a critique of your private dtor code here, feel
free to post it, in the meantime, the person giving you the
exam has apparently used wrong terminology.
 
V

Victor Bazarov

__PPS__ said:
in my exam in c++ I answered that destructors may be declared private,
public or protected.
The question was: "What possible scopes (public or private) can a
destructor have?"

'public' or 'private' are not scopes, they are _access_specifiers_.
I got 0 for this answer, they corrected me that destructor can only be
public.

Perhaps they meant a default destructor, the compiler-generated one...
When I showed the code with private destructor and friend functions
that create/destroy object they told me that I don't use it correctly,
that I don't call destructor etc etc....

So, what are the possible scopes destructors may have?

In your terms all of them!

Here is the code you should show:
----------------------------------------
#include <iostream>
class HasPublicDtor {
public:
~HasPublicDtor() { std::cout << "~HasPublicDtor()\n"; }
};

class HasProtectedDtor {
protected:
~HasProtectedDtor() { std::cout << "~HasProtectedDtor()\n"; }
};

class HasPrivateDtor {
friend class Friend;
private:
~HasPrivateDtor() { std::cout << "~HasPrivateDtor()\n"; }
};

class Derived: public HasProtectedDtor {
};

class Friend {
HasPrivateDtor d;
};

int main() {
HasPublicDtor def;
Derived d;
Friend f;
}
 
P

__PPS__

My code was even simpler, the entire program is like this:

#include <iostream>

using namespace std;

class test{
private:
test(){ cout << "create test obj" << endl;}
~test(){ cout << "deelted test object" << endl; }
public:
void doit(){ cout << "doit..." << endl; }
friend test* create(){ return new test(); }
friend void destroy(test* p){ delete p; }
};

int main(){
test *X;
X = create();
X->doit();
destroy(X);
}


But the teacher (that was so dumb, when I was arguing with him while
nobody in class understood what we where talikng about) was saying that
I violate access rights, overcome private and similar crap... basicly I
stoped to argue and said, "Tell me is test's destructor private?" He
said yes... Me: "So, why do you say it cannot be public?", he said that
you cannot now write like this:
test X;

Me: "But you also cannot call private functions, but doesn't make them
invalid etc..." He just didn't want to give up ...


In fact, I'm amazed by incompetence of the people who teach me, you may
view my old posts about them :))
http://tinyurl.com/bsf9v
Also, they corrected my "error" in this code:
class X{
public:
int s;
X(int size) : s(size){}
}

replacing line s(size){} with s=size{}, I'm not joking...
 
O

Old Wolf

__PPS__ said:
In fact, I'm amazed by incompetence of the people who teach me, you may
view my old posts about them :))
http://tinyurl.com/bsf9v
Also, they corrected my "error" in this code:
class X{
public:
int s;
X(int size) : s(size){}
}

replacing line s(size){} with s=size{}, I'm not joking...

Get a refund.
 
G

Greg Comeau

My code was even simpler, the entire program is like this:

#include <iostream>

using namespace std;

class test{
private:
test(){ cout << "create test obj" << endl;}
~test(){ cout << "deelted test object" << endl; }
public:
void doit(){ cout << "doit..." << endl; }
friend test* create(){ return new test(); }
friend void destroy(test* p){ delete p; }
};

int main(){
test *X;
X = create();
X->doit();
destroy(X);
}


But the teacher (that was so dumb, when I was arguing with him while
nobody in class understood what we where talikng about) was saying that
I violate access rights, overcome private and similar crap... basicly I
stoped to argue and said, "Tell me is test's destructor private?" He
said yes... Me: "So, why do you say it cannot be public?", he said that
you cannot now write like this:
test X;

Me: "But you also cannot call private functions, but doesn't make them
invalid etc..." He just didn't want to give up ...


In fact, I'm amazed by incompetence of the people who teach me, you may
view my old posts about them :))
http://tinyurl.com/bsf9v
Also, they corrected my "error" in this code:
class X{
public:
int s;
X(int size) : s(size){}
}

replacing line s(size){} with s=size{}, I'm not joking...

A few thoughts:
1) Remain polite and considerate (I'm sure you are, just remain so)
2) Consider going to the dept head. Telling them "my instructor
sucks" is useless. Give specifics or don't go. One problem
is enough for you to be tossed out the dept head's room,
even if the instructor was very wrong.
3) Be careful, because an issue will come up that you'll try
to say the instructor is wrong on because s/he has been
so often in the past, and it will turn out that you are
wrong, and then all the instructor has to say is "See!!"
4) Consider why you are in the program at this particular school
at all, whether this is an isolated case, etc.
5) If you involve other students in complaining, don't make it
an angry mob mentality, no matter how bad the situation.
 
B

Branimir Maksimovic

__PPS__ said:
My code was even simpler, the entire program is like this:

#include <iostream>

using namespace std;

class test{
private:
test(){ cout << "create test obj" << endl;}
~test(){ cout << "deelted test object" << endl; }
public:
void doit(){ cout << "doit..." << endl; }
friend test* create(){ return new test(); }
friend void destroy(test* p){ delete p; }
};

int main(){
test *X;
X = create();
X->doit();
destroy(X);
}


But the teacher (that was so dumb, when I was arguing with him while
nobody in class understood what we where talikng about) was saying that
I violate access rights, overcome private and similar crap... basicly I
stoped to argue and said, "Tell me is test's destructor private?" He
said yes... Me: "So, why do you say it cannot be public?", he said that
you cannot now write like this:
test X;

There are reasons to prevent instantianting auto objects,
for example "delete this" idiom, and private/protected constructor
or destructor is just a right tool for that.
Me: "But you also cannot call private functions, but doesn't make them
invalid etc..." He just didn't want to give up ...

This is just a bias in school. No need to argue, questions asked there
probably have written answers. So your thing is not to argue but just
to say/write whatever is already written :)
In fact, I'm amazed by incompetence of the people who teach me, you may
view my old posts about them :))

Gosipping is just bad.
Also, they corrected my "error" in this code:
class X{
public:
int s;
X(int size) : s(size){}
}

replacing line s(size){} with s=size{}, I'm not joking...

Well, that's their style, you are there to learn not to teach.
Same will be when you start to work. There are always rules,
coding styles etc which are not perfect, even can be really stupid,
but life goes on :)

Greetings, Bane.
 
M

Mike Wahler

Branimir Maksimovic said:
Well, that's their style,

So their 'style' is to teach invalid syntax?
you are there to learn not to teach.

But it sure would help if what he learns is
correct.
Same will be when you start to work. There are always rules,

Right. Such as the rules governing the language syntax.
coding styles etc which are not perfect, even can be really stupid,
but life goes on :)

This example wasn't coding style. It's a matter of
legal vs illegal syntax.

-Mike
 
B

Branimir Maksimovic

Mike Wahler said:
So their 'style' is to teach invalid syntax?

Well, I thought
X(int size):s(size){}
vs
X(int size){s=size;}

X(int size)s=size{} is just ridicilous, so I discarded it as posibillity.
In original program there is line with constructor and initializer
but there is no line s(size){}.
But it sure would help if what he learns is
correct.

Of course.
Right. Such as the rules governing the language syntax.


This example wasn't coding style. It's a matter of
legal vs illegal syntax.

I interpeted it differently, apology.

Greetings, Bane.
 
M

Mike Wahler

Branimir Maksimovic said:
Well, I thought
X(int size):s(size){}
vs
X(int size){s=size;}

X(int size)s=size{} is just ridicilous, so I discarded it as posibillity.
In original program there is line with constructor and initializer
but there is no line s(size){}.

Perhaps I should not have, but when I read:

I took OP's word for it that that's what they told him.
Of course.


I interpeted it differently, apology.

No apology needed, perhaps OP will clarify this.

-Mike
 
P

__PPS__

A few thoughts:
1) Remain polite and considerate (I'm sure you are, just remain so)
2) Consider going to the dept head. Telling them "my instructor
sucks" is useless. Give specifics or don't go. One problem
is enough for you to be tossed out the dept head's room,
even if the instructor was very wrong.
3) Be careful, because an issue will come up that you'll try
to say the instructor is wrong on because s/he has been
so often in the past, and it will turn out that you are
wrong, and then all the instructor has to say is "See!!"
4) Consider why you are in the program at this particular school
at all, whether this is an isolated case, etc.
5) If you involve other students in complaining, don't make it
an angry mob mentality, no matter how bad the situation.
--


That's really valuable list for every student IMO
and, off course, when I said I was arguing I really meant discussing it
in class, and when I talked about it I was really polite - what I write
in here is just brief explanation how it was in general; In fact, I
just mentioned that I was right and showed why, and the teacher started
this discussion himself. The guy who said that it was invalid use of
private dtor is the class tutor, the guy who marked my paper is another
person, the marker, and the class teacher/lecturer is the head of the
department. The thing is, I'm prety sure that her (the head's of the
dept) knowledge of c++ is really weak, aprox. on the level when I'd
been learing c++ for a year (I know it for ~5years). Basicly, she
writes c code, and not so good about c++. I just tried to talk to her,
and it's absolutely useless!!! I didn't even mention anything about
other teachers' knowledge, I just wanted to get the right mark for my
works (A+ always :)
And I don't involve other students into complaining!! I just have one
good friend in this class and only to him I say the truth :), that the
head of the dept sucks and why etc... and he told me some web site
(don't remember the name, something like myteachersucks.com or
whatever) where there are alot of completely negative posts about out
lecturer/dept head. And I don't attend her lectures anyways, they are
absolutely of no use for me (I only do assignments, quizes, exams). On
the first day I attended her lecture she started to explain classes and
said that it's soooo easy for her that she often forgets to tell
details to the students, so that students should ask questions
themselves about the stuff she forgets to mention :)...

I don't really care how well they teach etc, I study c++ from other
sources (groups, c++ faq are some good ones). What I care is that when
I answer right I got it marked as wrong... In previous quiz they asked
to write a function that accepts c-array of some structures
(Student{id,name,gpa}) and the size, and sort it based on the gpa. It
was stated that we may use any algorithm for sorting, so I used
std::sort :) fith binary predicate to compare Students (instead of
messing with loops etc as they expected students to do), but they
marked my solution as incorrect :)

And about my program, I'm in computer engeneering, concordia
university, Montreal, Canada. And now I'm thinking about switching to
some other program (not because of the teachers)
 
P

__PPS__

So their 'style' is to teach invalid syntax?
Well, I can't believe myself they mentioned that, probably they marked
it by mistake
about style... in most of the assignments we have style guidelines to
follow: separate interface from implementation and write
getters/setters for variables... :) Usually, I spent more time writing
getters-setters for variables than solving the problem itself
 
I

Ian

__PPS__ said:
Well, I can't believe myself they mentioned that, probably they marked
it by mistake
about style... in most of the assignments we have style guidelines to
follow: separate interface from implementation and write
getters/setters for variables... :) Usually, I spent more time writing
getters-setters for variables than solving the problem itself
In that case, your design is poor. Setters/getters should be regarded
as a design smell, ask yourself why one is required. Think about moving
the data to the object doing the getting, or moving the work to the
original container.

Ian
 
B

Branimir Maksimovic

__PPS__ said:
Also, they corrected my "error" in this code:
class X{
public:
int s;
X(int size) : s(size){}
}

replacing line s(size){} with s=size{}, I'm not joking...

Heh, just make s into const member in the future , then they can't do s=size
:)

Greetings, Bane.
 

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,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top