473,594 Members | 2,862 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 3941
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
14012
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
2126
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
4501
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
10277
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
2088
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
3252
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
3219
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
6662
by: pauldepstein | last post by:
#include <iostream> using namespace std; double & GetWeeklyHours() { double h = 46.50; double &hours = h; return hours; } //---------------------------------------------------------------------------
8
2206
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
7946
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
7877
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,...
0
8253
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
8240
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
5739
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
5411
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();...
1
2389
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
1
1482
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1216
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.