Declaring Template Class Variables - page 2

 
TraderTogami #:

What if attributes of the object are non-public? How would implementation change? Would I have to make "getter" and "setter" methods or is there another way? 

It will not change at all. - private or protected only means the access is limited to the inside of the scopes class, or (protected) its inheriters.

 
Dominik Christian Egert #:

I see, yes. I think your point is clear. But it should not be such a "dirty" hack. - Wouldnt it be better to stick to good practice when showing a solution. It might lead someone else to have false impressions on how to do things. - Especially when they cannot "subtract" the dirt from a shown code...

You are right. 

Originally I suggested an initializer method, like "Create" in "ChartObjects" or "Controls" classes in the standard library. 

 
Dominik Christian Egert #:

It will not change at all. - private or protected only means the access is limited to the inside of the scopes class, or (protected) its inheriters.

Okay, so I tried to follow your advice and made the two following methods: 


Queue(const Queue<T> queue){}

Queue<T> operator=(const Queue<T> queue){

    this.front = queue.front;
    this.rear = queue.rear; 
    this.max_size = queue.max_size; 
    this.curr_size = queue.curr_size; 
    ArrayResize(this.q, this.max_size); 
    for(int i = 0; i < this.max_size; i++){
        this.q[i] = queue.q[i]; 
    }
    Print("Copy Complete"); 
    return this;
}


Then I tried to run this code again: 

Queue<double> q; 
// Work
q = Queue<double>(20); // Where 20 is the max size of the queue

I get the same errors as before. However, if I do something like this: 

Queue<double> q;
Queue<double> r(20); 
r.Enqueue(1.0);  
// Work 
q = r; 

This works just fine and I confirmed the Print statement runs. What is the problem with the second block's declaration

 
TraderTogami #:

Okay, so I tried to follow your advice and made the two following methods: 



Then I tried to run this code again: 

I get the same errors as before. However, if I do something like this: 

This works just fine and I confirmed the Print statement runs. What is the problem with the second block's declaration

Could youshow the whole code, all relevant code. - I can only guess right now. - You do not have a parametric constructor. - Whats the compiler error?

EDIT:

Why is your copy constructor empty?

Queue(const Queue<T> queue){}


At least do this then:

Queue(const Queue<T>& queue){ operator=(queue); } 

If you do above, you should change the signature of the operator=() to this:


void operator=(const Queue<T>& queue)
 
Dominik Christian Egert #:

Could youshow the whole code, all relevant code. - I can only guess right now. - You do not have a parametric constructor. - Whats the compiler error?

EDIT:

Why is your copy constructor empty?


At least do this then:


If you do above, you should change the signature of the operator=() to this:


See the original post for all the relevant code and error strings. In terms of what is under "Work," currently it is nothing, and correction to the code that was shown, the methods should take in a reference of queue (of course, since anything else would be an error). 

 
Dominik Christian Egert #:

Could youshow the whole code, all relevant code. - I can only guess right now. - You do not have a parametric constructor. - Whats the compiler error?

EDIT:

Why is your copy constructor empty?


At least do this then:


If you do above, you should change the signature of the operator=() to this:


Instead of this: 

Queue<double> r(20); 

Queue<double> q(r);

He is trying to do this: 

Queue<double> q(Queue<double>(20));
 
TraderTogami:
q = Queue<double>(20);

Try this.

Queue<double> q(20);
 
Samuel Manoel De Souza #:

Try this.

I know you can do this, but say you did not know 20 was the max size of q and just want a general initialization 

Queue<double> q; 

Then assign q to be a Queue of 20 without adding a resize method (which is bad practice, since other generic classes don't have this workaround and resizing to 20 doesn't reset the variable). 



EDIT: Is "Queue<double>(20)" bad syntax? If so, what is the equivalent? 

 
TraderTogami #:

I know you can do this, but say you did not know 20 was the max size of q and just want a general initialization 

Then assign q to be a Queue of 20 without adding a resize method (which is bad practice, since other generic classes don't have this workaround and resizing to 20 doesn't reset the variable). 



EDIT: Is "Queue<double>(20)" bad syntax? If so, what is the equivalent? 

You have to use pointer. 

 
TraderTogami #:

I know you can do this, but say you did not know 20 was the max size of q and just want a general initialization 

Then assign q to be a Queue of 20 without adding a resize method (which is bad practice, since other generic classes don't have this workaround and resizing to 20 doesn't reset the variable). 



EDIT: Is "Queue<double>(20)" bad syntax? If so, what is the equivalent? 

How about making your queue dynamically adapt to the size of "inhabitants".

Grow it, if needed.

This way, you are not limited to a preknown size.

EDIT:

Maybe you could describe the precise behavior and use case you want. It's difficult to answer your requirements this way.
Reason: