473,396 Members | 1,961 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,396 software developers and data experts.

setting member class

Hi All,

If you have the following class:
class foo
{
private:
int offset;
public:
void SetOffset(int InOffset);
};

foo::SetOffset(int InOffset)
{
offset = InOffset;
}

foo::DoSomething(int offset, int......)
{
// I can set offset two ways
SetOffset(InOffset); //1

offset = inOffset; //2

}
The question, within the class, should I call SetOffset, or just set
the private member. I know calling SetOffset causes an overhead (
function call), but you can say that if we make the SetOffset inline,
that will eliminate the overhead of calling function.

I always set the class memeber directly without the need to call the
function "SetOffset", but I ran into code that calls the function
instead of just changing the value of the class attributes. Any
suggestions why somebody would do that?
Question 2)

Have you seen something like this in the class declaration? why would
you do so?
foo(foo * pfoo); // foo is the class name

Thanks for the feedback.

Oct 5 '06 #1
3 1974

Osama Aouda wrote:
Hi All,

If you have the following class:
class foo
{
private:
int offset;
public:
void SetOffset(int InOffset);
// missing:
void DoSomething(int, int);
};

foo::SetOffset(int InOffset)
void foo::SetOffset(int InOffset)
{
offset = InOffset;
}

foo::DoSomething(int offset, int......)
void foo::DoSomething(int offset, int......)
{
// I can set offset two ways
SetOffset(InOffset); //1

offset = inOffset; //2

}
The question, within the class, should I call SetOffset, or just set
the private member. I know calling SetOffset causes an overhead (
function call), but you can say that if we make the SetOffset inline,
that will eliminate the overhead of calling function.
What if SetOffset(...) has another purpose like limiting the offset
value allowed? Or maybe verifying the supplied parameter's validity in
some other way?t

void foo::SetOffset(int n)
{
offset = (n < 10) ? n : 10; // max = 10
}

try it.
>
I always set the class memeber directly without the need to call the
function "SetOffset", but I ran into code that calls the function
instead of just changing the value of the class attributes. Any
suggestions why somebody would do that?
What if you derive from class foo and would like the integer to remain
private?
What if foo is a member of another class and you need to have its
offset modified from the default?
class bar
{
foo f;
public:
void setOffset(int n)
{
// f.offset = n; // not allowed
f.SetOffset(n);
}
};
>

Question 2)

Have you seen something like this in the class declaration? why would
you do so?
foo(foo * pfoo); // foo is the class name
Imagine foo to be a node that you are inserting into a linked list.
pfoo might be a pointer to the next node in the list, which might be
null if its the last node.
>
Thanks for the feedback.
Oct 5 '06 #2
SetOffset(InOffset); //1
This might be required if you need to perform certain
operations/validations on the InOffset variable before assigning it to
member variable. If you make this inline, the overhead will not be
there, if the function just sets the value. And if the function
performs additional tasks then it will ensure that the code is not
repeated throughout the program.

foo(foo * pfoo); // foo is the class name
This type of code is usually required with recursive data structures,
where an object needs to store pointer to another object of the same
class. e.g. linked list OR parent-child structure. Take a look at
decorator design pattern, a similar code is required for its
implementation.
Amir Kamerkar

Oct 5 '06 #3
Thank you all for the reply.

Osama Aouda

am******@yahoo.com wrote:
SetOffset(InOffset); //1
This might be required if you need to perform certain
operations/validations on the InOffset variable before assigning it to
member variable. If you make this inline, the overhead will not be
there, if the function just sets the value. And if the function
performs additional tasks then it will ensure that the code is not
repeated throughout the program.

foo(foo * pfoo); // foo is the class name

This type of code is usually required with recursive data structures,
where an object needs to store pointer to another object of the same
class. e.g. linked list OR parent-child structure. Take a look at
decorator design pattern, a similar code is required for its
implementation.
Amir Kamerkar
Oct 5 '06 #4

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

Similar topics

6
by: bluekite2000 | last post by:
I want to accomplish the following void foo(const T &v1, const T &v2) { if(v1&v2 is of type double) double alpha=1.0; else if (v1&v2 is of type complex double) complex double alpha=(1.0,1.0);...
0
by: Cat | last post by:
I have class Base, and class Derived. I am serializing Derived objects from XML, and these Derived objects are allowed to 'inherit' the values from other named Base objects already defined in...
9
by: Alphonse Giambrone | last post by:
I have built a simple composite control that consists of a textbox, requiredfieldvalidator and rangevalidator. For properties that are unique to the individual control, I set/get them directly...
0
by: David J | last post by:
Hi, I am strugling with the propertygrid and a listbox. I am using the universaldropdowneditor from the codeproject (code below). However I am populating the listbox via a datasource. The problem...
8
by: pauldepstein | last post by:
I am writing a program which looks at nodes which have coordinates which are time-dependent. So I have a class called node which contains the private member declarations int date; int month; ...
2
by: Avinash | last post by:
I have a class defined as follows: class main { ........ protected: another_class *obj; public: void set_another_class(another_class *ptr);
6
by: Spoon | last post by:
Hello everyone, Consider a class with many integer members. I want to set every bit in all members to 0 in the constructor. struct Foo { Foo() { memset(this, 0, sizeof *this); } int a, b,...
2
by: bruestle | last post by:
I've been working on improving my javascript coding and decided to start using classes. I've gotten pretty far but am having problems attaching a member function to a control event. ...
3
by: kjell | last post by:
Hi, I wonder if someone may have a solution to a small problem I'm experiencing with a C++ program? I'm have two classes in my application that provides methods for setting parameters in the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.