Function pointer problem

A

Adrian

Example A fails to compile with
exampleA.cpp: In member function 'void A::a() const':
exampleA:10: error: no matching function for call to 'A::b(const
std::string&) const'
exampleA:17: note: candidates are: virtual void (* A::b(const
std::string&))()const <near match>

Example B compiles fine.

Now I assume that the const is not binding to what I think it should
in the example A

Adding the typedef fixes this

Can anyone tell me what is going on


Adrian

+++++++++++++ Example A +++++++++++++++
#include <iostream>
#include <string>

class A
{
public:
void a() const
{
const std::string arg("test");
void(*func_ptr)(void)=b(arg);
if(func_ptr)
{
}
};
virtual ~A() throw();

virtual void (*b(const std::string &str))(void) const=0;
};

int main(int argc, char *argv[])
{
return 0;
}

+++++++++++++ Example B +++++++++++++++
#include <iostream>
#include <string>

class A
{
public:
typedef void(*func_ptr)(void);
void a() const
{
const std::string arg("test");
func_ptr func_ptr=b(arg);
if(func_ptr)
{
}
};
virtual ~A() throw();

virtual func_ptr b(const std::string &str) const=0;
};

int main(int argc, char *argv[])
{
return 0;
}
 
M

modemer

Example A fails to compile with
exampleA.cpp: In member function 'void A::a() const':
exampleA:10: error: no matching function for call to 'A::b(const
std::string&) const'
exampleA:17: note: candidates are: virtual void (* A::b(const
std::string&))()const <near match>

Example B compiles fine.

Now I assume that the const is not binding to what I think it should
in the example A

Adding the typedef fixes this

Can anyone tell me what is going on

Adrian

+++++++++++++ Example A +++++++++++++++
#include <iostream>
#include <string>

class A
{
public:
void a() const
{
const std::string arg("test");
void(*func_ptr)(void)=b(arg);
if(func_ptr)
{
}
};
virtual ~A() throw();

virtual void (*b(const std::string &str))(void) const=0;

This prototype means function b returns a "void *" rather than a
function pointer, this causes the type mismatching error in line 10
};

int main(int argc, char *argv[])
{
return 0;

}

+++++++++++++ Example B +++++++++++++++
#include <iostream>
#include <string>

class A
{
public:
typedef void(*func_ptr)(void);
void a() const
{
const std::string arg("test");
func_ptr func_ptr=b(arg);
if(func_ptr)
{
}
};
virtual ~A() throw();

virtual func_ptr b(const std::string &str) const=0;

};

int main(int argc, char *argv[])
{
return 0;}
 
B

Branimir Maksimovic

Example A fails to compile with
exampleA.cpp: In member function 'void A::a() const':
exampleA:10: error: no matching function for call to 'A::b(const
std::string&) const'
exampleA:17: note: candidates are: virtual void (* A::b(const
std::string&))()const <near match>

Example B compiles fine.

Now I assume that the const is not binding to what I think it should
in the example A

Adding the typedef fixes this

Can anyone tell me what is going on

Adrian

+++++++++++++ Example A +++++++++++++++
#include <iostream>
#include <string>

class A
{
public:
void a() const
{
const std::string arg("test");
void(*func_ptr)(void)=b(arg);
if(func_ptr)
{
}
};
virtual ~A() throw();

virtual void (*b(const std::string &str))(void) const=0;

Are you sure that this is not syntax error? This looks like try
to define pointer to function and declare pure virtual function
in the same time. This syntax works for me:

virtual void(* b(const std::string &str)const)(void)=0;

Greetings, Branimir.
 
A

Adrian

Branimir said:
Are you sure that this is not syntax error? This looks like try
to define pointer to function and declare pure virtual function
in the same time. This syntax works for me:

virtual void(* b(const std::string &str)const)(void)=0;

I was unsure where to put the const and tried a few places. Thats works
fine and makes sense.

Wow dont function pointers make for readable code!! :)

Thanks
 
K

Keith Halligan

Example A fails to compile with
exampleA.cpp: In member function 'void A::a() const':
exampleA:10: error: no matching function for call to 'A::b(const
std::string&) const'
exampleA:17: note: candidates are: virtual void (* A::b(const
std::string&))()const <near match>

Example B compiles fine.

Now I assume that the const is not binding to what I think it should
in the example A

Adding the typedef fixes this

Can anyone tell me what is going on

Adrian

+++++++++++++ Example A +++++++++++++++
#include <iostream>
#include <string>

class A
{
public:
void a() const
{
const std::string arg("test");
void(*func_ptr)(void)=b(arg);
if(func_ptr)
{
}
};
virtual ~A() throw();

virtual void (*b(const std::string &str))(void) const=0;

};

int main(int argc, char *argv[])
{
return 0;

}

+++++++++++++ Example B +++++++++++++++
#include <iostream>
#include <string>

class A
{
public:
typedef void(*func_ptr)(void);
void a() const
{
const std::string arg("test");
func_ptr func_ptr=b(arg);
if(func_ptr)
{
}
};
virtual ~A() throw();

virtual func_ptr b(const std::string &str) const=0;

};

int main(int argc, char *argv[])
{
return 0;

}

The reason the compiler freaks out in the first example is because of
the const. The function pointer is called in a constant member, and
when we make a call to a function from inside a constant member, to
another member function, the this pointer will get put on the stack as
expected, but with a constant member a constant this member gets put
(because its a constant function we are not allowed to modify our
classes data).

The const is in the wrong place and the signature of the virtual
function and the calling function do not match. I think as others
have said if you move the const to inside the brackets before the
(void), it will compile. It did for me anyway on gcc 3.4.
 
J

James Kanze

On May 9, 7:12 pm, Adrian <[email protected]> wrote:

[...]
Are you sure that this is not syntax error? This looks like try
to define pointer to function and declare pure virtual function
in the same time.

The pointer to function is the return value. On the other hand,
I don't think that the final const is legal; it seems to apply
to a non-member function whose address is returned.
This syntax works for me:
virtual void(* b(const std::string &str)const)(void)=0;

Which is the same, except that you've moved the const to apply
to the member function.
 
A

Adrian

The reason the compiler freaks out in the first example is because of
the const. The function pointer is called in a constant member, and
when we make a call to a function from inside a constant member, to
another member function, the this pointer will get put on the stack as
expected, but with a constant member a constant this member gets put
(because its a constant function we are not allowed to modify our
classes data).

The const is in the wrong place and the signature of the virtual
function and the calling function do not match. I think as others
have said if you move the const to inside the brackets before the
(void), it will compile. It did for me anyway on gcc 3.4.- Hide quoted text -

I couldnt find where to place the const. I wanted a const member
function that returns a function ptr to an external C function that
has no operations on the object itself. Moving the const worked as
stated before - I just dont like it when the typedef works and the
code without doesnt because I dont understand what it going on. Now
you guys explained it makes sense and I realize the const was in the
wrong place.
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top