473,804 Members | 3,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Returning a reference to a class variable

I have a class which contains (1) a class variable and (2) a method
(e.g., methodA) which returns a reference to the class variable. If
the variable has been assigned a value by a constant initializer in
the "var" statement, my httpd process crashes with a segmentation
fault when I call methodA. If the variable is assigned a value in one
of the class methods, the httpd process does not crash when methodA is
called and the value returned is the expected value. This is true
even if the variable was first initialized in the "var" statement, as
long as its value is subsequently set in a class method.

Fails:
class C {
var $v = "hello";

function C () {
}

function &methodA () {
return ($this->v);
}
}

Succeeds:
class C {
var $v = "hello";

function C () {
$this->v = "hello";
}

function &methodA () {
return ($this->v);
}
}

(Please note that my test case is too complicated to post. I included
the above examples to illustrate my point, but have not tested them.)

Has anyone else seen this behavior? Any insight would be appreciated.
Jul 17 '05 #1
2 3950
from php manual:

In PHP 4, only constant initializers for "var" variables are allowed. To
initialize variables with non-constant values, you need an initialization
function which is called automatically when an object is being constructed
from the class. Such a function is called a constructor.
"nielson" <ni*****@spacel ines.com> wrote in message
news:f4******** *************** **@posting.goog le.com...
I have a class which contains (1) a class variable and (2) a method
(e.g., methodA) which returns a reference to the class variable. If
the variable has been assigned a value by a constant initializer in
the "var" statement, my httpd process crashes with a segmentation
fault when I call methodA. If the variable is assigned a value in one
of the class methods, the httpd process does not crash when methodA is
called and the value returned is the expected value. This is true
even if the variable was first initialized in the "var" statement, as
long as its value is subsequently set in a class method.

Fails:
class C {
var $v = "hello";

function C () {
}

function &methodA () {
return ($this->v);
}
}

Succeeds:
class C {
var $v = "hello";

function C () {
$this->v = "hello";
}

function &methodA () {
return ($this->v);
}
}

(Please note that my test case is too complicated to post. I included
the above examples to illustrate my point, but have not tested them.)

Has anyone else seen this behavior? Any insight would be appreciated.

Jul 17 '05 #2
Thanks for the response, but my question was not how to initialize a
class variable, but why returning a reference to a class variable
causes my httpd process to crash if the variable was assigned a value
only in a "var" statement. If the variable was assigned a value in a
class method, all executes without error and as expected. Has anyone
else seen similar behavior?

".bucho" <do******@comtv .ru> wrote in message news:<bk******* ****@bucho.lan> ...
from php manual:

In PHP 4, only constant initializers for "var" variables are allowed. To
initialize variables with non-constant values, you need an initialization
function which is called automatically when an object is being constructed
from the class. Such a function is called a constructor.
"nielson" <ni*****@spacel ines.com> wrote in message
news:f4******** *************** **@posting.goog le.com...
I have a class which contains (1) a class variable and (2) a method
(e.g., methodA) which returns a reference to the class variable. If
the variable has been assigned a value by a constant initializer in
the "var" statement, my httpd process crashes with a segmentation
fault when I call methodA. If the variable is assigned a value in one
of the class methods, the httpd process does not crash when methodA is
called and the value returned is the expected value. This is true
even if the variable was first initialized in the "var" statement, as
long as its value is subsequently set in a class method.

Fails:
class C {
var $v = "hello";

function C () {
}

function &methodA () {
return ($this->v);
}
}

Succeeds:
class C {
var $v = "hello";

function C () {
$this->v = "hello";
}

function &methodA () {
return ($this->v);
}
}

(Please note that my test case is too complicated to post. I included
the above examples to illustrate my point, but have not tested them.)

Has anyone else seen this behavior? Any insight would be appreciated.

Jul 17 '05 #3

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

Similar topics

6
14037
by: Krackers | last post by:
How do you write a function which returns a reference to an array. I can only get a function to return a copy of the array itself. I've had a look at some other threads in this group an the return value of a function acts like 'by Val' returning the value only (except for objects) can you make it return a reference instead? cheers, Krackers
18
2147
by: cppaddict | last post by:
Hi, Is it considered bad form to have the subscript operator return a const reference variable? If not, what is the proper way to do it? My question was prompted by the code below, my problematic attempt to implement a subscript operator that returns a const reference. The dubious code is marked at the end. <code>
13
4521
by: Matthias Kaeppler | last post by:
Hi, I was wondering why library implementors often make getter functions return strings by value (copies). For example, in boost::filesystem the leaf() function returns an std::string by value. So does Gnome::Vfs::FileInfo::get_name(). Isn't that unnecessary overhead? I could as well return by reference to const-string and avoid
10
10298
by: Fraser Ross | last post by:
I need to know the syntax for writing a reference of an array. I haven't seen it done often. I have a class with a member array and I want a member function to return an reference to it. Returning a pointer to the first element might do but I want to do what I've said. Fraser.
2
2104
by: Cory Burkhardt | last post by:
I am not sure what the suggested practice is for returning reference types from read-only properties. Consider an example: Suppose I have a Point class that is a reference type. I use the Point class from within a Shape class. I want the user of the Shape class to be able to see the location of the Shape but not be able to change it. So I declare a read-only property: class Shape { private Point location; /* other Shape stuff */
4
3267
by: Naren | last post by:
Hi , Is it correct to return reference of a member variable. because it can be like we have already deleted the object and still holding the reference of member variable. A *a = new A; int & r = a.get(); // int& A::get() { //returning a member of class A}
18
3247
by: Dejfson | last post by:
Dear All, can someone clarify me how to return the reference to the empty object in case of error? _not working_ Example of what i'd like to do: const MyClassData& MyClass () { QListIterator<MyClassDatait(fancylist)
7
6686
by: pauldepstein | last post by:
#include <iostream> using namespace std; double & GetWeeklyHours() { double h = 46.50; double &hours = h; return hours; } //---------------------------------------------------------------------------
8
2221
by: darren | last post by:
Hi everybody, have a quick look at this code: ===== ===== int main(void) { string msg; makeString(msg); cout << "back in main, result = " << msg << endl;
0
9706
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10320
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10077
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...
0
9150
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7620
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...
1
4299
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3820
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2991
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.