473,394 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Passing member variables as function parameters

Is this a good design for myClass in the example below? Are there
better alternatives for m_oA and myCallback?
struct apiA, and function apiFn () are not in my control, as they are
standard api supplied by the exchange for which I am writing the
application. Also the member variable m_oA logically belongs to myClass

struct apiA {

int a;

int b;

} ;

typedef void (apiCallback) (apiA* pA);

void apiFn (apiA *pA, apiCallback *pCbk)

{

std::cout << pA->a << " " << pA->b << std::endl;

pA->a = 8;

pA->b = 9;

std::cout << pA->a << " " << pA->b << std::endl;

//Simple illustration only. In real life callback fn will be
called later pA->a = 18;

pA->b = 19;

(*pCbk)(pA);

return;

}

class myClass {

apiA m_oA;

public:

void UseapiFn () ;

static void myCallback (apiA *pA) ;

} ;

void

myClass::UseapiFn ()

{

m_oA.a =6;

m_oA.b =7;

apiFn (&m_oA, myCallback);

}

void myClass::myCallback (apiA *pA)

{

std::cout << "In callback " << pA->a << " " << pA->b <<
std::endl;
return;

}

int main ()

{

myClass oInst;

oInst.UseapiFn();

return 0;

}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jun 20 '06 #1
6 2024
Ninan wrote:
Is this a good design for myClass in the example below? Are there
better alternatives for m_oA and myCallback?
I don't see any problem with it. What makes you ask? You must have
doubts about the validity of the design, why?

Yes, 'm_oA' is a private member, but the API function doesn't know
(nor does it care) where the object comes from.
[...]


V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 20 '06 #2
In article <11**********************@p79g2000cwp.googlegroups .com>,
Ninan <ni****@yahoo.com> writes
Is this a good design for myClass in the example below? Are there
better alternatives for m_oA and myCallback? struct apiA, and function
apiFn () are not in my control, as they are standard api supplied by
the exchange for which I am writing the application. Also the member
variable m_oA logically belongs to myClass


This post is a wonderful example of how too much whitespace is as bad
(or possibly worse) as too little. I do not have the time to reformat
the code and without doing that I find it unreadable.

I am pretty sure that the problem (every line of source code has an
extra blank line added to it) is not an artefact of my news-reader but
an artefact of cutting and pasting from an incompatible source code
editor.

As the moderator who accepted this post my hands were tied because
format is not a reason for rejection, but perhaps readability ought to
be. We can reject for excessive source code, perhaps we should also have
an allowance for rejection for too many blank lines:-) What do others
think?

--
Francis Glassborow ACCU
Author of 'You Can Do It!' and "You Can Program in C++"
see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/
youcandoit/projects
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jun 20 '06 #3

Ninan wrote:
Is this a good design for myClass in the example below? Are there
better alternatives for m_oA and myCallback?
struct apiA, and function apiFn () are not in my control, as they are
standard api supplied by the exchange for which I am writing the
application. Also the member variable m_oA logically belongs to myClass


I'm not really sure what you are asking. You say that we can't change
the struct, nor the function that accepts the struct and the callback
pointer. Also, you say that m_oA must belong to myClass. It's as if
you said "here's some code that I can't change, what should I change?"

You are properly calling the apiFn() supplied and properly passing
arguments to it. I see nothing, given your restrictions, that should
change.

joshua lehrer
http://www.lehrerfamily.com/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jun 21 '06 #4
Francis Glassborow wrote:
In article <11**********************@p79g2000cwp.googlegroups .com>,
Ninan <ni****@yahoo.com> writes
Is this a good design for myClass in the example below? Are there
better alternatives for m_oA and myCallback? struct apiA, and
function apiFn () are not in my control, as they are standard api
supplied by the exchange for which I am writing the application.
Also the member variable m_oA logically belongs to myClass


This post is a wonderful example of how too much whitespace is as bad
(or possibly worse) as too little. I do not have the time to reformat
the code and without doing that I find it unreadable.

I am pretty sure that the problem (every line of source code has an
extra blank line added to it) is not an artefact of my news-reader but
an artefact of cutting and pasting from an incompatible source code
editor.

As the moderator who accepted this post my hands were tied because
format is not a reason for rejection, but perhaps readability ought to
be. We can reject for excessive source code, perhaps we should also
have an allowance for rejection for too many blank lines:-) What do
others think?


The message was x-posted to c.l.c++ and c.l.c++.m. It appeared at the
same time in both. It seems that it has to be approved for c.l.c++.m
in order to appear even in c.l.c++.

I am sure we're not going to solve the x-posting "problem" at this time,
but I would consider it unfair if a posting doesn't get to c.l.c++ if
moderators of c.l.c++.m decide to reject it for c.l.c++.m (whatever the
reason, and however justified it might be). Just a thought.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jun 21 '06 #5
Francis Glassborow wrote:
In article <11**********************@p79g2000cwp.googlegroups .com>,
Ninan <ni****@yahoo.com> writes
Is this a good design for myClass in the example below? Are
there better alternatives for m_oA and myCallback? struct
apiA, and function apiFn () are not in my control, as they
are standard api supplied by the exchange for which I am
writing the application. Also the member variable m_oA
logically belongs to myClass
This post is a wonderful example of how too much whitespace is
as bad (or possibly worse) as too little. I do not have the
time to reformat the code and without doing that I find it
unreadable.
Well, it's one of the easiest formatting problems to correct:-).
I am pretty sure that the problem (every line of source code
has an extra blank line added to it) is not an artefact of my
news-reader but an artefact of cutting and pasting from an
incompatible source code editor. As the moderator who accepted this post my hands were tied
because format is not a reason for rejection, but perhaps
readability ought to be. We can reject for excessive source
code, perhaps we should also have an allowance for rejection
for too many blank lines:-) What do others think?


It's a sad fact of life that some of the tools we use mangle
formats. I don't think that there's anyway to avoid it
completely. Code without any indentation is worse than the
extra blank lines, and at one time, Google stripped all leading
white space when posting. Once I realized this, I started
inserting a '|' at the start of each line in a code example.
But sometimes I'd forget, and of course, there were a lot of
postings before I'd realized that there was a problem. In this
case, while the problem might be in the copy/paste, as you
suggest, I wouldn't put it past his newsreader to have inserted
an extra empty line before any line which didn't start in the
first column. In which case, I couldn't possibly have been
aware of the problem before seeing the posting appear.

And if you reject because of extra lines, what about because of
wrapped lines---things like:

std::cout << "In callback " << pA->a << " " << pA->b <<
std::endl;

or

//Simple illustration only. In real life callback fn will be
called later pA->a = 18;

I find that this plays even more havoc to readability,
especially when what is wrapped is part of a comment. And
there's an awful lot of newsreader software which silently
wraps. Sometimes just visibly, so the poster thinks everything
is formatted correctly, but the posting contains lines with a
couple of hundred characters, or more.

So while I agree with you that it's not nice, and I think that
we should lobby newsreader providers to do the right thing, I
don't think it reasonably, today, to reject such postings.

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jun 21 '06 #6
Victor Bazarov wrote:
Francis Glassborow wrote:
[...]
As the moderator who accepted this post my hands were tied
because format is not a reason for rejection, but perhaps
readability ought to be. We can reject for excessive source
code, perhaps we should also have an allowance for rejection
for too many blank lines:-) What do others think?

The message was x-posted to c.l.c++ and c.l.c++.m. It
appeared at the same time in both. It seems that it has to be
approved for c.l.c++.m in order to appear even in c.l.c++. I am sure we're not going to solve the x-posting "problem" at
this time, but I would consider it unfair if a posting doesn't
get to c.l.c++ if moderators of c.l.c++.m decide to reject it
for c.l.c++.m (whatever the reason, and however justified it
might be). Just a thought.


That's the way newsgroups work, by definition. If a message is
cross posted, it must be approved by the moderators in every
moderted group it is posted to in order for it to appear
anywhere. A cross posted message is a single message that is
either present or absent on a server; if it is present, it is
visible in all of the groups it is cross-posted to. Moderation
occurs upstream, and determines whether a message will be
propagated to the servers or not.

--
James Kanze GABI Software
Conseils en informatique orient��e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S��mard, 78210 St.-Cyr-l'��cole,
France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jun 24 '06 #7

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

Similar topics

5
by: Newsgroup - Ann | last post by:
Gurus, I have the following implementation of a member function: class A { // ... virtual double func(double v); void caller(int i, int j, double (* callee)(double)); void foo() {caller(1,...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
6
by: Adam Hartshorne | last post by:
Hi All, I have the following setup. Two 'std::vector's which i iterate through in a for (iterate through vector1 of types X) { for (iterate through vector2 of types Y) { f(x) }
5
by: rettigcd | last post by:
Hello, I need to create a custome form that operates similar to the MsgBox() and InputBox() functions. I can't figure out how to pass data to a MODAL dialog box (form). I've tried 3...
12
by: Dennis D. | last post by:
Hello: I want a function to return three variables to the calling procedure: Private Function CalcTimes(ByVal iAddDays as Integer, ByVal iAddHours as Integer, ByVal iAddMins as Integer) As...
17
by: Christopher Benson-Manica | last post by:
Does the following program exhibit undefined behavior? Specifically, does passing a struct by value cause undefined behavior if that struct has as a member a pointer that has been passed to...
50
by: Mikhail Teterin | last post by:
Hello! The sample program below is compiled fine by gcc (with -Wall), but rejected by Sun's SUNWspro compiler (version 6 update 2). The point of contention is, whether a value for one of the...
18
by: tbringley | last post by:
I am a c++ newbie, so please excuse the ignorance of this question. I am interested in a way of having a class call a general member function of another class. Specifically, I am trying to...
7
by: amygdala | last post by:
Hi all, I'm starting this new project in which I'ld like to implement sort of a design pattern I have seen being used in the CodeIgniter framework. Basically, the site will examine the URI and...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.