Pointers and references

hello
I have some questions regarding pointers and reference.

1. in the code that shown, can I delete the new object using
delete &a;
?

(suppose class 1 is a class that I created).

If that line has a problem(can cause memory leak?), can someone write the right way to write it?

 
class1 a = *(new class1());


2.
If I have reference to some variable, does it have the same address of the variable or not? (if I compare the reference to the "original" variable)

3.If I receive a reference to some object as a paramter in method, is it possible to delete the object itself through the method?


Thanks a lot.
The result of class1 a = *(new class1()); is the same as class1 a;
So no delete &a; isn't valid


If I have reference to some variable, does it have the same address of the variable or not? (if I compare the reference to the "original" variable)
Either you compare the pointer or the content of the object. Provide code

3.If I receive a reference to some object as a paramter in method, is it possible to delete the object itself through the method?
Yes, but better not.
If that line has a problem(can cause memory leak?), can someone write the right way to write it?

class1 a = *(new class1());

The problem is that this line creates two objects, each of type class1. The first one is created with new and the pointer to it is lost (memory leak), the second one is created with the name a.

To fix, create just one object:
class1 a;
thank you.
another question:

What would be the result it this code:

const int foo(int x,int y) {code...}
int z;
z=foo(a,b);

is z is now const and can't be changed after?

does z has to be declared as a const?

2.
what would be the result of:
void foo(int x, int y) const {code...}
what is const-safe?

3. virtual destructor.
What would happen if I wouldn't declare destructor of main class as virtual.
and called for destructor of one of the subclasses?
something like that:
1
2
3
4
5
6
7
8
9
10
11
12
class Customer{
protected:
string name
...
 }

class RegularCustomer :public Customer
{
private: 
int numberOfProducts;

}


and I would write:
1
2
customer a = new RegularCustomer();
delete a;


would I delete only the fields that are found in Customer class and not all the fields that added in RegularCustomer (I just gave a really basic example) ?
Last edited on
What would be the result it this code:
const int foo(int x,int y) {code...}

there are no non-class const rvalues in C++. That const is ignored (some compilers give warnings:
gcc:
test.cc:1:26: warning: type qualifiers ignored on function return type [-Wignored-qualifiers]
 const int foo(int x,int y) {return x+y;}


clang:
est.cc:1:1: warning: 'const' type qualifier on return type has no effect [-Wignored-qualifiers]
const int foo(int x,int y) {return x+y;}


intel:
test.cc(1): warning #858: type qualifier on return type is meaningless
  const int foo(int x,int y) {return x+y;}


int z;
...
is z is now const

it can never be const; types do not change at runtime. int cannot become const int or vice versa.


what would be the result of:
void foo(int x, int y) const {code...}

this would fail to compile unless foo is a class member function

3. virtual destructor.
What would happen if I wouldn't declare destructor of main class as virtual.
and called for destructor of one of the subclasses?

The behavior of such program is undefined; anything at all can happen, there are no guarantees or expectations.
thank you man.

const int foo(int x,int y) {code...}
and can't function return a const value?

void foo(int x, int y) const {code...}
so there is nothing called "const safe"?

both of these functions are declared as part of class functions.



and can't function return a const value?
It can, but this way it's a copy anyway so it doesn't matter

so there is nothing called "const safe"?
Why not? The const after the function head says that the member of the class cannot be modified
Topic archived. No new replies allowed.