Constructor calls in initialization lists
Hello!
I've encountered a difference in behavior between Solaris Studio 12 update 1 (CC: Sun C++ 5.10) and g++ that has me baffled.
This is seen when a class's constructor features an initialization list before the start of its body. It seems that when I use a constructor call to initialize a member variable in the list, Solaris Studio calls that constructor once for the temporary object that is between parentheses and a second time to initialize my class's member variable with a copy constructor using the former temporary object. g++ by contrast calls that constructor only once.
Here's a testcase that should hopefully clarify my point. The initialization list is in class Foo's constructor:
File: "foo.h"
#include <iostream>
#include <stdlib.h>
#include <string.h>
class Bar {
size_t length;
public:
Bar(){
std::cout << "Bar no args constructor. This: " << this << std::endl;
Bar(size_t length){
std::cout << "Bar length constructor. This: " << this << std::endl;
Bar( const Bar& ) {
std::cout << "Bar copy constructor. This: " << this << std::endl;
virtual ~Bar(){
std::cout << "Bar destructor. This:" << this << "\n";
class Foo {
private:
int alpha;
Bar bravo;
public:
Foo(int length);
};File: "foo.cpp"
#include "foo.h"
Foo::Foo(int len): bravo(Bar(len)) {
alpha=len;
}File: "testcase.cpp"
#include "foo.h"
#include <unistd.h>
int main( int argc, char** argv) {
Foo testcase(4);
usleep(2000000);
std::cout << "Program end of life" << std::endl;
return EXIT_SUCCESS;
}When I compile these files with Solaris Studio...
$ CC -library=Crun,Cstd -o ./foo.o -c ./foo.cpp ; CC -library=Crun,Cstd -o ./sol_studio_testcase ./testcase.cpp ./foo.o...the testcase program produces this output:
$ ./sol_studio_testcase
Bar length constructor. This: 8047744
Bar copy constructor. This: 804777c
Bar destructor. This:8047744
Program end of life
Bar destructor. This:804777cBy contrast, when I use g++...
g++ -o ./gfoo.o -c ./foo.cpp; g++ -o ./gplusplus_testcase ./testcase.cpp ./gfoo.o...the output shows only one call to a contructor for class Bar:
Bar length constructor. This: 0x8047784
Program end of life
Bar destructor. This:0x8047784I'm working on a fairly large g++ codebase that makes heavy use of initialization lists throughout and relies on g++'s behavior in this regard (there's some vital memory management baked in the constructors and destructors).
So would anyone here know if/how I could induce Solaris Studio's CC to behave like g++ in this case?
Best regards,
- Matt Boyer
In the initialization
Foo::Foo(int len): bravo(Bar(len)) { ... } the program semantics call for creating an anonymous temp object of type Bar that is used to initialize the bravo member of Foo, and later destroyed.
The C++ standard allows, but does not require, an implementation to eliminate the temp object in some circumstances. The Sun/Oracle C++ compiler does not currently have this optimization.
But there is no reason in this case to write bravo(Bar(len))+, because bravo is already of type Bar. If you write the simpler code bravo(len)+, no temp object is implied or created by the compiler.
Similar Messages
-
Constructor Calls...
I have read that the default constructor calls itself in each and every case. And it calls its super class constructor. Is it true?
1- What are the cases in which default constructor is not needed?
2- Is it Object's class default constructor that finally initializes instance variables or something else?not exactly.
this is what happens:
1. if there is an explicit call to another constructor with this(..) execute it.
else if there is an explicite call to super(...) call the constructor of the super class
else if we are not of type Object call super().
2. init instance variables with explicitly given default values
3. execute code in constructor. -
API call that will list the meetings of employees that are assigned to a manager
Hi,
What is the API call that will list the meetings of employees that are assigned to a manager? The manager will be logged in and when I use the API call:
http://[serverid]/api/xml?&action=report-my-meetings&principal-id=[##]
I always get the same results which is a list of the meetings for the manager. If put the id of the employee, I still get the managers meetings.
I have looked through all of the API documentation and cannot find which call I could make to get the results that I want. The documentation specifically says that report-my-meetings provides information about meetings that the logged in user is scheduled to attend. So how do I get the information for the managers employees?
What is needed is the Name, Duration, and Start Time of all meetings that the employee is enrolled in who is assigned to the manager that is logged in.
Thanks for any help with this!I posted this in the wrong forum and have moved it to the XML forum.
-
Multiple times constructors calling in Myfaces
Hi
i m fasing multiple times constructor calling for myfaces programs
i have created a small web appl. with one text box and one submit button, to test this behaviour.
i have used this sample to
get web page
enter some text value
submit the page
and atlast page get returned to me
i don't understand the multiple times consturctor calling and exact sequence of Getter/Setter calling?
Following are details -
faces-config.xml
<managed-bean>
<managed-bean-name>fileUploadBean</managed-bean-name>
<managed-bean-class>com.dbschenker.dts.model.backingbean.FileUpload</managed-bean-class>
<manged-bean-scope>request</manged-bean-scope>
</managed-bean>
jsp file
<h:inputText id="txtSample" value="#{fileUploadBean.txtName}"/>
<h:commandButton action="#{fileUploadBean.uploadFile}"
image="../../images/upload_0.png"
onmouseover="this.src='../../images/upload_0.png'"
onmouseout="this.src='../../images/upload_1.png'"
onclick="return true;"/>
Backing bean
public class FileUpload {
private String txtName;
public void setTxtName(String txtName) {
System.out.println("Calling Setter... ");
this.txtName= txtName;
public String getTxtName() {
System.out.println("Calling Getter... ");
return txtName==null?"":this.txtName;
public FileUpload() {
System.out.println("Calling Constructor... ");
public String uploadFile() {
System.out.println("Upload Form Submitted... ");
return "";
Output to Console
Calling Constructor...
Calling Getter...
Calling Constructor...
Calling Constructor...
Calling Getter...
Calling Constructor...
Calling Setter...
Calling Constructor...
Upload Form Submitted...
Calling Constructor...
Calling Getter...Hi
that blog was really good and in depth...
but i have one real time problem with multiple time constructor calling
if you replace text box with file upload tag in my earlier sample program and then following is out put ...
Calling Constructor...
getUploadedFile()...
Calling Constructor...
getUploadedFile()...
Calling Constructor...
setUploadedFile()...
Uploaded File Name - D:\AsnUploadTemplate02.XLS
Calling Constructor... ------------------(.1
Upload Form Submitted...
java.lang.NullPointerException
at com.dbschenker.dts.model.backingbean.FileUpload.uploadFile(FileUpload.java:40)
----------- trailing exception stack trace...
Calling Constructor...
getUploadedFile()...
form this output u can find that my UploadedFile backing bean object gets null just before form submit method...
whereas its has been properly instantiated in setter method,
if constructor (1 wasn't call then object (uploadedFile) can be found in method (FileUpload.uploadFile)
i have also tried to make scope of backing bean Session , but constructor, getter & setter calling sequence doesn't different even after.
i have also considered both of your blog as ---
http://balusc.blogspot.com/2006/09/debug-jsf-lifecycle.html
http://balusc.blogspot.com/2008/02/uploading-files-with-jsf.html
but my problem stands as it is
kindly help -
Oracle Object Type Constructor Called Multiple Times
I have an object type with a custom constructor. In SQL, when I reference the attributes the constructor is called multiple times in Oracle 11.2.0.4.
Why is the constructor called multiple times?
How do I stop it?
My current work around is to reference the attributes and use the /*+ materialize */ hint.
Problem Setup
create or replace type Foo as object
Bar1 NUMBER,
Bar2 NUMBER,
Bar3 NUMBER,
CONSTRUCTOR FUNCTION Foo(p_Bar1 NUMBER, p_Bar2 NUMBER, p_Bar3 NUMBER)
RETURN SELF AS RESULT
DETERMINISTIC
create or replace type body Foo is
-- Member procedures and functions
CONSTRUCTOR FUNCTION Foo(p_Bar1 NUMBER, p_Bar2 NUMBER, p_Bar3 NUMBER)
RETURN SELF AS RESULT
DETERMINISTIC
AS
BEGIN
SELF.Bar1 := p_Bar1;
SELF.Bar2 := p_Bar2;
SELF.Bar3 := p_Bar3;
dbms_output.put_line('Foo Constructor Called');
RETURN;
END;
end;
Problem
-- Constructor is called 6 times!
-- Once for each column and once for each predicate in the where clause.
SELECT x.f.bar1 AS bar1, x.f.bar2 AS bar2, x.f.bar3 AS bar3, f
FROM (
SELECT foo(p_Bar1 => 1, p_Bar2 => 2, p_Bar3 => 3) f
FROM dual d
) x
WHERE x.f.bar1 = x.f.bar1 AND x.f.bar2 = x.f.bar2
Output
Foo Constructor Called
Foo Constructor Called
Foo Constructor Called
Foo Constructor Called
Foo Constructor Called
Foo Constructor Called
Workaround
-- Work Around
-- Constructor is called 3 times
-- Once for each column in the inline view.
-- Note, I removed column f (the object type) because it's not compatible with the materialize hint.
WITH y AS (
SELECT /*+ materialize */ x.f.bar1 AS bar1, x.f.bar2 AS bar2, x.f.bar3 AS bar3
FROM (
SELECT foo(p_Bar1 => 1, p_Bar2 => 2, p_Bar3 => 3) f
FROM dual d
) x
SELECT y.bar1, y.bar2, y.bar3
FROM y
WHERE y.bar1 = y.bar1 AND y.bar2 = y.bar2Another work-around is described in this thread... Accessing fields of a custom object type... which makes use of a collection type combined with the TABLE operator, like so...
create or replace type FooTable as table of Foo;
SELECT x.bar1 AS bar1, x.bar2 AS bar2, x.bar3 AS bar3, value(x) f
FROM table(FooTable(
foo(p_Bar1 => 1, p_Bar2 => 2, p_Bar3 => 3)
)) x
WHERE x.bar1 = x.bar1 AND x.bar2 = x.bar2
BAR1 BAR2 BAR2 F
1 2 3 (1, 2, 3)
Foo Constructor Called
Hope that helps...
Gerard -
Constructor calls in BlazeDS?
I have a Java POJO that accesses a database. It establishes a connection to the database in the constructor, the uses that connection in each call to query the database.
However, the query time is VERY slow, PAINFULLY slow. I suspect that the constructor is being called each time the query method is called.
I am new to BlazeDS, so i am curious about a few things:
1- is the constructor called each time i make a call to the remote object?
2-is it possible to instantiate the object and keep it in memory so i can make references to the instantiated object instead?
3-Would this be facilitated using EJB or Spring Framework? I am looking into these, but don't know enough yet to make the call myself.
ANY information you can provide is GREATLY appreciated....sorry, i REALLY need this answered, if possible, so i'm going to BUMP it just this once!
-
Hi all,
I am calling ALV block list twice. In I block list user selects any record and then clicks on a button and then control goes to II block list. My problem is that when i come back from II to I list, and select any new record, the selected values in internal table passed in I list are not reflected in that table.
I think this problem is because we are displaying different tables in these 2 lists. When we go to II list, the ALV block list is gettting reinitialized and on coming back, the above problem occurs.
If you have any inputs, please let me know.
Thanks in advance.
Regards,
Ridhima.Hi all,
I am calling ALV block list twice. In I block list user selects any record and then clicks on a button and then control goes to II block list. My problem is that when i come back from II to I list, and select any new record, the selected values in internal table passed in I list are not reflected in that table.
I think this problem is because we are displaying different tables in these 2 lists. When we go to II list, the ALV block list is gettting reinitialized and on coming back, the above problem occurs.
If you have any inputs, please let me know.
Thanks in advance.
Regards,
Ridhima. -
Call waiting says "multiple calls" rather than listing the number or the contact. This error is not consistent, sometimes it says "multiple calls" and other times it lists the contact. This is happening on a coworker's iPhone 4s. Does anyone know how to get it to list the contact every time?
Thank you,
DanIt sounds as though it may not be ending calls correctly, I would try restarting the phone, and then resetting all settings.
-
Dynamic constructor call with arguments
hey all i am quite new to this dynamic creation stuff
basically i have an array of class literals i.e One.class Two.class Three.class
etc
i then loop through this array using the isInstance() method to find the objects correct type
when i find this type i want to then create an object of this type with arguments passed to the constructor
i know there is a way to create the object using a defualt non arg constructor call using newInstance()...
so to repeat is there a way to dynamically create an object while passing arguments to the constructor?
thanx robCall getContstructor() on the class passing it an array of argument types. Then call newInstance() on the Constructor object that gets returned passing it an array of the arguments.
-
Parent constructor calls abstract method
Hi everybody!
I'm wondering if there is something wrong with java or if the idea is just too ill?
Anyway, I think it would be great if this hierachy would work...
Two classes A and B.
Class A defines an astract method.
In A's constructor this abstract method is called.
Class B extends A and provides an implementation for the abstract method in A.
Class B also defines a member variable that is set in B's implementation of the abstract method.
In class' B constructor the parent constructor A() is called.
example:
public abstract class A {
public A() {
createComponents();
public abstract void createComponents();
public class B extends A {
private String string = null;
public B() {
super();
System.out.println("B::B() " + string);
public void createComponents() {
System.out.println("B::createComponents() begin");
string = new String("test");
System.out.println("B::createComponents() " + string);
public void describe() {
System.out.println("B::describe() " + string);
public static void main(String[] args) {
B b = new B();
b.describe();
}running the code above produces the following output:
B::createComponents() begin
B::createComponents() test
B::B() null
B::describe() null
why is the string member variable null in B's constructor??
thanks in advance
Peter Bachl
Polytechnic University of Upper Austria, Hagenberg
[email protected]The answer is that the call of the super-constructor
is allways done before the initialization
of the member variable. That's all and that's the
normal behavior.
order :
- initialization of static variables
- call to the super-constructor
- initialization of the instance variables
- execution of the constructor
Since this is the advanced forum it is relevant to point out that that is not exactly true.
There is a step in java that 'initializes' member variables before any constructors are called, super or other wise.
From the JLS 12.5...
Otherwise, all the instance variables in the new object, including those declared in superclasses, are initialized to their default values (4.5.5)
Using the following code as an example
class MyClass
int i1;
int i2 = 1;
.When creating an instance of the above there will be three 'initializations'
// Part of object creation...
i1 = 0; // The default value
i2 = 0; // The default value
// Part of construction...after super ctors called.
i2 = 1; // The variable initializer (see step 4 of JLS 12.5)
Unfortunately the descriptions are rather similar and so confusion can result as to when the assignment actually occurs. -
Is it possible to override constructor and instance initializer?
In the source below, I found that all 4 System.out.println() messages are printed.
Is it possible to have the constructor and instance intializer in ClassC overrides that of ClassB?
import java.util.*;
class ClassB {
System.out.println("Hello");
ClassB() {
System.out.println("HaHa");
class ClassC extends ClassB {
System.out.println("Hello1");
ClassC() {
System.out.println("HaHa1");
public class Test1 {
public static void main(String[] args) {
ClassC c = new ClassC();
}You can't override an instance initializer, no. And when you create an object, some constructor of each of its superclasses must be called to do the creating. In your example, it's the default constructor of ClassB that's called. ClassC's constructor must call some constructor of ClassB.
-
[OO Concept] Make a parent class perform code after constructor calls?
Hello,
I want to do something like this:
class Parent {
Parent(){
// do some parent stuff
private void postConstruction(){
// parent and all subclasses have completed construction, now do some post-construction code
// analyze some of the child construction stuff
class Child extends Parent{
Child(){
// do some child stuff
public static void main(String[] args){
Child child = new Child();
}I assume I am having this problem because of bad OO design. What would be the problem way to design this so that the parent can check the values of its members after all of its subclasses have been constructed?
Thanks..You can certainly call the postConstruction() method from your Parent constructor. But then (as you can see) it will be called before anything in your Child constructor. Bad design? Yes, bad design of the Java language in my opinion. It should have allowed the user to control the order of initialization more. But it doesn't. So you can't do that. Which basically means you can't use the constructor to construct your objects. You'll have to call some other method where you aren't required to call super.postConstruction() at the beginning.
-
Calling function from list of values section?
can i call a function from list of values(LOV) section as well? I know we can sql query but udf is supported?
See this recent thread: Display as Text (LOV) or join
Scott -
Start a call with a list with one click
Hello.
Don't know whether this is the right place, but: i've just created a list in Skype. What i hoped to do, is start a call with all the people in that list with one click. I'm using Skype on Mac/OSX, and can't find such an option (right clicking the list does not supply that option, starting a new conversation and trying to add the list seems impossible ....). Seems i have to add every individual from the list. Am i missing a feature? Or is this a feature request ?
Kindest regards,
RaoulFound it: http://www.cisco.com/en/US/customer/docs/voice_ip_comm/jabber/Windows/9_1/JABW_BK_CA48EE46_00_cisco-jabber-for-windows-administration.html
-
JLabel constructor call problem
Hello,
I've problem with calling JLabel's constructor with passing String value to constructor.
jobject jobjLabel;
jclass JLabelClass;
jmethodID mid;
JLabelClass = (*env)->FindClass(env,"javax/swing/JLabel");
if(JLabelClass == NULL) {
printf("Can't find class JLabel\n");
goto destroy;
mid = (*env)->GetMethodID(env,JLabelClass,"<init>","(Ljava/lang/String;)V");
jobjLabel = (*env)->NewObject(env,JLabelClass,mid,"Hello World");At line:
jobjLabel = (*env)->NewObject(env,JLabelClass,mid,"Hello World");Main Thread exits with error code 1. I am sure that this line is true but I didn't find problem.
Are there any methods to find errors before program exit?Probably I've found. I must pass constructor value as String object. Am I wrong?
But Are there any methods to find our errors before program exit?
Thanks in Advance
Maybe you are looking for
-
Notifications center and Facebook.
Hi guys, I've been trying to figure out the awesome features of OS X mountain lion's notification center. Is it possible to post to a Facebook fan page from notification center? I think yes but the groups list is only limited. Can I update this list?
-
How do I register my ipad to a different computer
Could anybody give me advice on how to I register and sync my iPad2 to a new computer. I get a message that my iPad2 is alredy sync'd to another library that I do not have acess to but I simply want to do a full backup of my iPad2 to ensure that all
-
Serial Number in report name aswell as report body ?
Hello I am fairly new to TS, I am using the Single Pass Execution entry point with a sequence I have written. I have written my own LabVIEW based Barcode Scanning Module for getting the DUT Serial Number from the user (As the Single Pass doesn't incl
-
Change database connection for FRS Report
currently we are using ASO BSO model and all FRS reports are retrieve against BSO Database.Now we are converting to ASO only model. can i use the same report against ASO database . There is a option of chang database connection under tool in workspac
-
Help! my curve wont turn on!
my phone was on charge and i was listening to music on it and then the screen went blank. Now it wont turn on i have done a battery pull but nothing happened! i connected it to the computer but nothing happened! what do i do?