About pointer to member function of derived class - c++

Here's my code, and the IDE is DEV C++11
#include<iostream>
using namespace std;
class A{
public:
int a=15;
};
class B:public A
{
};
int main(){
int A::*ptr=&B::a; //OK
int B::*ptr1=&A::a; //why?
int B::A::*ptr2=&B::a;//why?
int B::A::*ptr3=&A::a; //why?
}
I have read Programming Languages — C++ and I know the type of &B::a is int A::*, but I don't realise why the next three lines will pass the compilation.
And the weirdest thing to me is the syntax of int B::A::* , what's the meaning of this? I'm just a newcomer of C/C++, so please tolerate my weird question.

Diagram representation may help you understand why it is ok and compiles
Interesting will be once you reinitialize the same variable in inherited class
#include<iostream>
using namespace std;
class A {
public:
int a = 15;
};
class B :public A
{
public:
int a = 10;
};
int main() {
int A::*ptr = &B::a; //Waring class B a value of type int B::* cannot be
//used to initialize an entity of type 'int A::*'
int B::*ptr1 = &A::a; // why?
int B::A::*ptr2 = &B::a;//Waring class B a value of type int B::* cannot
// be used to initialize an entity of type 'int A::*'
int B::A::*ptr3 = &A::a; //why?
}

Related

Accessing a variable using namespace and a template class

I can't access the variable width.
The A.h file:
namespace AN{
template <typename T> class A{
public:
unsigned int width; #The variable
...
}
}
The B.cpp file:
#include "A.h"
using namespace AN;
namespace BN{
bool something(){
unsigned int * w = AN::&width;
}
}
I tried also AN::A::&width but it doesn't work as well.
This has nothing to do with templates. It's about classes and objects. The address of width is determined by the object that it's a part of; without an object, there is no width.
However, without an object you can create a pointer-to-member; it's not an ordinary pointer (if it was, it would be called "pointer"). Like this:
class A {
public:
int width;
};
int A::*w = &A::width;
You use it to access that variable when you create an object:
A a;
a.*w = 3;
A aa;
aa.*w = 4;
If you really only want one value of width for every object of your type, yes, you can make it a static member:
class A {
public:
static int width;
};
int A::width;
Now you can create a pointer to that member as an ordinary pointer:
int* w = &A::width;
and you can use w as an ordinary pointer:
*w = 3;

C++ Request for member, which is of non-class type (when using class template, can't define in the main)

**On my main i can't add a note on my new Object of the Class Trabalho
ass.add_nota(num);
**
There is a error on my compilation.
My "Trabalho.h" code:
#include <string>
#include <vector>
#include <iostream>
//#include "Enunciado.h"
//#include "Pessoa.h"
using namespace std;
class Estudante;
class Enunciado;
template <class T>
class Trabalho{
static int id_auxiliar;
string texto;
int ano;
int id;
vector<float> calif;
T* Enun;
vector<Estudante*> estudantes;
vector<Enunciado*> enunciados;
public:
Trabalho();
Trabalho(string texto, vector<Estudante*> est, T* en, int ano);
~Trabalho();
void set_texto(string texto);
string get_texto();
void add_nota(float nota);
void add_enun(Enunciado* en){Enun = en;};
int get_id(){return id;};
int get_ano() {return ano;};
void reutilizar(int id_enun);
vector<float> get_calif() {return calif;};
vector<Estudante*> get_estudantes() {return estudantes;};
Enunciado* get_enunciado() {return Enun;};
};
#endif
And my main code:
int main(int argc, char const *argv[]) {
int n;
int m;
Pesquisa ah();
float num = 1.1;
Trabalho<Pesquisa> ass();
Trabalho<Pesquisa>* tass = new Trabalho<Pesquisa>();
ass.add_nota(num);
tass->add_nota(num);
#ifndef ENUNCIADO_H_
#define ENUNCIADO_H_
#include "trabalho.h"
#include "Pessoa.h"
#include <string>
using namespace std;
class Enunciado
{
static unsigned int id_auxiliar;
const unsigned int id;
string titulo;
string descricao;
vector<int> anos_utilizados;
static unsigned int max_util;
public:
Enunciado(string titulo, string descricao);
virtual ~Enunciado();
int get_id(){return id;};
void set_titulo(string titulo);
string get_titulo();
void set_descricao(string descricao);
string get_descricao();
vector<int> get_anos_utilizados();
void mod_max_util(int a);
};
class Pesquisa: public Enunciado{
vector<string> ref;
public:
Pesquisa(string tit, string des, vector<string> refe);
};
class Analise: public Enunciado{
vector<string> repositorios;
public:
Analise(string tit, string des, vector<string> repos);
};
class Desenvolvimento: public Enunciado{
public:
Desenvolvimento(string tit, string des);
};
#endif
Both ways when i create a new Trabalho when i define my type (pesquisa is a class type on #include "Enunciado.h".
This is the two erros that appears:
"Description Resource Path Location Type
request for member 'add_nota' in 'ass', which is of non-class type 'Trabalho()' Test.cpp /Trabalho1/src line 42 C/C++ Problem
"
And:
Description Resource Path Location Type
Method 'add_nota' could not be resolved Test.cpp /Trabalho1/src line 42 Semantic Error
Can anyone help?
Thank you !
Your error is in trying to call the default constructor as
Pesquisa ah();
or
Trabalho<Pesquisa> ass();
Unfortunately, C++ is very misleading in this and it would declare your variable ass of type Trabalho<Pesquisa>(), which means "a function of zero arguments returning Trabalho<Pesquisa>" and that's exactly that the compiler error says: a function type is not a class type and as such does not have the member add_nota. Indeed, it does look exactly like a function declaration, if you look at it that way:
int main();
^ ^ ^
type arguments
name
It's a very common mistake, especially for those coming from a Java background. But it can easily catch a C++ programmer off guard as well. More information can be found here or here or here, you can see that the same error message has perplexed a good many people.
If you have a compiler conforming to the C++11 language revision, try replacing all those occurrences by
Trabalho<Pesquisa> ass{};
If not, just leave
Trabalho<Pesquisa> ass;
Unlike in Java, this does not mean that the variable will stay uninitialized. It's the C++ way to call a default (zero-argument) constructor.

Initialization by constructor in c++

I have question about constructor,
why following code works correctly:
#include <iostream>
using namespace std;
class mycl
{
private:
int a;
//struct
//{
char b,c;
//} ms;
public:
mycl (int _a,char _b,char _c):a (_a), b (_b), c (_c){}
};
int main() {
// your code goes here
mycl slc (15, 'a', 'f');
return 0;
}
https://ideone.com/wBgM1b
but there is a compilation error in this one
https://ideone.com/Yqxvzk
is it possible to initialize members of complex types this way?
p.s.
thank for translate and for answer.
sorry about wrong language
You want:
mycl(int _a, char _b, char _c) : a(_a), ms{_b, _c} {}
// ^^^^^^^^^^

why static data members must have to be defined outside of class and why not with static constant data members ?

First question:-
why this is must have to be defined static data members outside of the class and why not this same concept follows in static constant data members?
Example for static data members:-
#include<iostream>
using namespace std;
class game
{
static int num;
int i;
public:
void foo()
{
cout<<endl<<num<<endl;
}
};
int game::num=0;
int main()
{
game g;
g.foo();
return(0);
}
Example for static constant data members:-
#include<iostream>
using namespace std;
class game
{
static const int num;
int i;
public:
void foo()
{
cout<<endl<<num<<endl;
}
};
int game::num=0; \\error why ?
int main()
{
game g;
g.foo();
return(0);
}
second question:-
Static constant data members initialization is allowed only for integral types why not for strings ?
#include<iostream>
using namespace std;
class game
{
static const char name[10]="vikas"; \\ error why ?
int i;
public:
void foo()
{
cout<<endl<<name<<endl;
}
};
int main()
{
game g;
g.foo();
return(0);
}

C++ static functions and variables

I have written a class as shown below:
#include<iostream>
using namespace std;
class A
{
static int cnt;
static void inc()
{
cnt++;
}
int a;
public:
A(){ inc(); }
};
int main()
{
A d;
return 0;
}
I want to call the function inc through the constructor, but when i compile i am getting an error as:
/tmp/ccWR1moH.o: In function `A::inc()':
s.cpp:(.text._ZN1A3incEv[A::inc()]+0x6): undefined reference to `A::cnt'
s.cpp:(.text._ZN1A3incEv[A::inc()]+0xf): undefined reference to `A::cnt'
I am unable to understand what the error is... plz help...
Static field is not defined - Take a look at Why are classes with static data members getting linker errors?.
#include<iostream>
using namespace std;
class A
{
static int cnt;
static void inc(){
cnt++;
}
int a;
public:
A(){ inc(); }
};
int A::cnt; //<---- HERE
int main()
{
A d;
return 0;
}
Inside the class static int cnt; is only declared, and need to be defined. In C++ you usually declare in your .h .hpp files and then define your static class members in your .c and .cpp files.
In your case, you need to add
int A::cnt=0; // = 0 Would be better, otherwise you're accessing an uninitialized variable.