473,738 Members | 4,774 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Casting a class method in a constructor

Hi all,

I have come up against a problem when I am registering a callback
within a constructor. The code that I am writing contains a class that
deals with everything to do with bringing up a window in the OS (I'm
just trying to encapsulate everything that is particularly OS specific
in my code).

When the class is created the constructor is called and part of the
goodness of window creation involves registering a callback that deals
with messages passed between the OS and the window. The particular
callback code is a member of this window class.

It all goes something like this ....

class win {

public:
win::win();
LRESULT CALLBACK win::msghandler (arg1,arg2..);

}

win::win()
{
....
windowClass.lpf nWndProc = (WNDPROC)msghan dler;
....
}

LRESULT CALLBACK win::msghandler (arg1,arg2..)
{
....
}
Hopefully I have made it clear that the way that the callback member
function is used is that it is cast to another type as part of the
constructor's window initialization.

Now the core of this code has all worked fine and dandy in C for me,
but when I port it to C++ it fails and it appears that it fails when
the compiler attempts to make the cast. I know that you should be able
to call member functions in constructors (and in this case it shouldnt
even be a problem with it not being initialized as it is merely being
registered for later use). The compiler I am using is MSVC 6.0 and (if
you hadnt guessed) its a win 32 system - but I dont think that the OS
is the problem here.

How do I get around the inability to compile?
- is it my lack of knowledge of C++ (I only recently have made the
transition from C :-). Ouch. )
- is it the compiler?

I dont want to split the constructor up into several functions as this
kind of violates the reasons for using C++ in the first place, and
though I can force it to work by declaring the callback as a static I
create other problems for myself elsewhere.

much thanks to everyone for your help and a happy new year - Mathew

Jan 1 '07 #1
2 1743
On Mon, 01 Jan 2007 14:52:30 -0800, tendots wrote:
Hi all,

I have come up against a problem when I am registering a callback
within a constructor. The code that I am writing contains a class that
deals with everything to do with bringing up a window in the OS (I'm
just trying to encapsulate everything that is particularly OS specific
in my code).
I'm strongly suspect you'll be much better off posting to an OS-specific
newsgroup:

http://www.parashift.com/c++-faq-lit...t.html#faq-5.9
When the class is created the constructor is called and part of the
goodness of window creation involves registering a callback that deals
with messages passed between the OS and the window. The particular
callback code is a member of this window class.

It all goes something like this ....

class win {

public:
win::win();
LRESULT CALLBACK win::msghandler (arg1,arg2..);

}

win::win()
{
...
windowClass.lpf nWndProc = (WNDPROC)msghan dler; ... }

LRESULT CALLBACK win::msghandler (arg1,arg2..) { ... }
Not terribly helpful, because I haven't a clue from your code what LRESULT
or CALLBACK or WNDPROC or windowClass or ... are.
Hopefully I have made it clear that the way that the callback member
function is used is that it is cast to another type as part of the
constructor's window initialization.
But what you _haven't_ made clear is what the cast-from and cast-to
types actually are. This may well be relevant. BTW the code you post uses
a C style cast which may be valid C++ but may not behave the same in C++
as in C.
Now the core of this code has all worked fine and dandy in C for me, but
when I port it to C++ it fails
Fails to run? Fails to compile?
and it appears that it fails when the
compiler attempts to make the cast.
Ah. Fails to compile. And the error message is...?
I know that you should be able to
call member functions in constructors (and in this case it shouldnt even
be a problem with it not being initialized as it is merely being
registered for later use). The compiler I am using is MSVC 6.0 and (if
you hadnt guessed) its a win 32 system - but I dont think that the OS is
the problem here.

How do I get around the inability to compile? - is it my lack of
knowledge of C++ (I only recently have made the transition from C :-).
Ouch. )
Ummm... possibly
- is it the compiler?
Ummm... probably not

The problems are:

1) possibly wrong newsgroup, but not really sure, since
2) insufficient information supplied.

Please read the FAQ:

http://www.parashift.com/c++-faq-lite/how-to-post.html

before posting here.

--
Lionel B
Jan 2 '07 #2

<te*****@hotmai l.co.ukwrote in message
news:11******** *************@s 34g2000cwa.goog legroups.com...
Hi all,

I have come up against a problem when I am registering a callback
within a constructor. The code that I am writing contains a class that
deals with everything to do with bringing up a window in the OS (I'm
just trying to encapsulate everything that is particularly OS specific
in my code).

When the class is created the constructor is called and part of the
goodness of window creation involves registering a callback that deals
with messages passed between the OS and the window. The particular
callback code is a member of this window class.

It all goes something like this ....

class win {

public:
win::win();
LRESULT CALLBACK win::msghandler (arg1,arg2..);

}

win::win()
{
...
windowClass.lpf nWndProc = (WNDPROC)msghan dler;
...
}

LRESULT CALLBACK win::msghandler (arg1,arg2..)
{
...
}
Hopefully I have made it clear that the way that the callback member
function is used is that it is cast to another type as part of the
constructor's window initialization.

Now the core of this code has all worked fine and dandy in C for me,
but when I port it to C++ it fails and it appears that it fails when
the compiler attempts to make the cast. I know that you should be able
to call member functions in constructors (and in this case it shouldnt
even be a problem with it not being initialized as it is merely being
registered for later use). The compiler I am using is MSVC 6.0 and (if
you hadnt guessed) its a win 32 system - but I dont think that the OS
is the problem here.

How do I get around the inability to compile?
- is it my lack of knowledge of C++ (I only recently have made the
transition from C :-). Ouch. )
- is it the compiler?

I dont want to split the constructor up into several functions as this
kind of violates the reasons for using C++ in the first place, and
though I can force it to work by declaring the callback as a static I
create other problems for myself elsewhere.

much thanks to everyone for your help and a happy new year - Mathew
The Microsoft Windows "Window Procedure" function cannot be a nonstatic
member function. Make it either a static member function or a non-member
function.

See the first three items at
http://www.parashift.com/c++-faq-lit...o-members.html

They don't discuss your exact issue, but similar ones of the same concept.

-Mike
Jan 2 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
1931
by: JustSomeGuy | last post by:
I have two object types ClassA and ClassB class ClassA { public: int data; operator ClassB() { ClassB b; b.data = data + 1; return (b);
3
1680
by: Kurt | last post by:
i just can't figure out why something im doing is not working correctly.... public interface IInterface { int someProperty { get; set; }
23
3522
by: René Nordby | last post by:
Hi there, Is there anyone that knows how to do the following? I have a class A and a class B, that 100% inherits from class A (this means that I don't have other code in class B, than the Inherit statement).
8
9180
by: David Williams | last post by:
Hi all, I have a templated Vector3D class which holds (x,y,z) components as the specified type. I quite often wish to cast a Vector3D holding ints into a Vector3D holding floats and vice versa. Like so: Vector3D<int> intVec(10,20,30); Vector3D<float> floatVec = intVec; Of course this doesn't work. I would be happy if instead the following
6
1458
by: Spoon | last post by:
Hello everyone, I'm writing code where I can receive two kinds of packets. The first 12 octets are common to both packet types, i.e. they have the same semantics. The 1st type has 4 more octets in the header, then the payload. The 2nd type has 16 more octets in the header, then the payload. I was thinking I'd write a base class with two derived classes:
7
13697
by: S. Lorétan | last post by:
Hi guys, Sorry for this stupid question, but I don't know why it isn't working. Here is my (example) code: namespace Test { class A { public string Label1; }
3
1538
by: per9000 | last post by:
Hi, can I reach a hidden method when doing ugly inheritance in python? .... def spin(self, n): print "A", n .... .... def spin(self, m): print "B", m .... .... def spin(self, k): print "C", k ....
19
2233
by: jan.loucka | last post by:
Hi, We're building a mapping application and inside we're using open source dll called MapServer. This dll uses object model that has quite a few classes. In our app we however need to little bit modify come of the classes so they match our purpose better - mostly add a few methods etc. Example: Open source lib has classes Map and Layer defined. The relationship between them is one to many. We created our own versions (inherited) of Map...
5
2660
by: petethebloke | last post by:
I'm not sure how to ask for what I want, so I can't find it on Google. I'll explain..... I have a class 'hanging' with a constructor: class hanging{ function hanging(){ }
0
9476
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9335
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9208
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6751
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6053
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2745
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.