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. 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.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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>
|
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
|
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.
|
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 */
| |
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}
|
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)
|
by: pauldepstein |
last post by:
#include <iostream>
using namespace std;
double & GetWeeklyHours()
{
double h = 46.50;
double &hours = h;
return hours;
}
//---------------------------------------------------------------------------
|
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;
|
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...
|
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,...
| |
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,...
|
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...
|
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...
|
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...
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |