Accessing a member variable's address in derived class.Behavior change when member is having different access specifier
Date : March 29 2020, 07:55 AM
may help you . Short answer: There is no undefined behavior involved. The behavior you see is: The expression &A::a is an attempt to obtain a pointer to member pointing to member a of class A. If a is protected in A, this expression only passes access checks within class A (or friends of A). In a class B derived from A, you can get the same pointer to member only via the expression &B::a (note that the type of this expression will still be int A::*). So: if A::a is protected in A, the expression &A::a is not allowed in a member function of derived class B. This is your compiler error. if A::a is public in A, this expression is valid, producing a pointer to memeber. Streaming a pointer to member to an ostream, for example using cout << &A::a will print 1. This results from invoking ostream::operator << (bool). You can use the boolalpha manipulator to see that this is indeed the chosen overload: cout << boolalpha << &A::a will print true. If you use the modified expression &(A::a) or simply &a, no pointer to member is formed. Here the address of member a of the current object (i.e the same as &(this->a)) is taken, which is a regular pointer to int. This access to a protected member of a base class subobject of *this is valid, so that variant can be used even if a is protected in A.
|
Javascript class member function calling member function by setInterval, couldn't access member variable
Date : March 29 2020, 07:55 AM
I hope this helps . setInterval() doesn't work with this. See here: https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setInterval#The_this_problem function job_queue(){
this.job = null;
this.pointer = this.job;
this.job_dispatcher = null;
this.length = 0;
}
job_queue.prototype.add_job = function( job ){
if( this.job == null ){
console.log('1st');
this.job = {
job:job,
next:null
};
this.pointer = this.job;
this.length = 1;
}else{
console.log('2nd');
this.pointer.next = {
job:job,
next:null
};
this.pointer = this.pointer.next;
this.length++;
}
};
job_queue.prototype.event_handler = function(){
if( typeof this.job['job'] == 'undefined'){
console.log('??');
}
if( this.job.job != null ){
console.log('hi');
this.job.job();
this.job = this.job.next();
}
}
job_queue.prototype.start_dispatch = function(){
var self = this;
if( this.job_dispatcher == null ){
console.log( this.event_handler );
this.job_dispatcher = setInterval( function(){self.event_handler()},1000);
}
}
var jq = new job_queue();
function a(){
console.log('hi');
};
function b(){
console.log('hi2');
}
jq.add_job(a);
jq.add_job(b);
jq.add_job(a);
jq.start_dispatch();
|
Error when using public member function to access private member variable: Variable "was not declared in this scope
Tag : cpp , By : user123585
Date : March 29 2020, 07:55 AM
wish helps you Update for anyone having trouble returning multidimensional arrays , You forgot the Card:: int (*Card::palette())[3]{
return _palette;
}
class Card {
private:
static int _palette[][3];
public:
static int (*palette())[3];
};
|
Access a member function of an object that is a member variable of a parent class
Date : March 29 2020, 07:55 AM
This might help you Dont instantiate a seperate parent class, it will be instantiated as part of instantiating the child class. Also pass the object to the child instantiation and create a __construct() method and pass the parameter on to it. class Child extends Parent
{
public __construct($var)
{
parent::__construct($var);
}
public function doStuff()
{
return parent::$object->objectFunction());
}
}
public function control()
{
//$parent = new Parent(new Object($variable));
$child = new Child(new Object($variable)); // line 25
$child->doStuff();
}
|
How can I access member variables of a vector of objects that is a member variable from a class?
Date : March 29 2020, 07:55 AM
I wish did fix the issue. I do know how to access member variables given a vector of objects but suppose float value = n.getnn()[i].getval()[j][k];
|