473,326 Members | 2,023 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,326 software developers and data experts.

C++ constructor differs from java

21
Hi,

I was just browsing some java code and found that it is possible in java to call a constructor within another constructor. The rule being that the it should be first line in the constructor and also there shouldn't be any duplication.Both these rules have valid reasons. But why can't we do the same in c++.

Java Code :

public class c {

public c(){}
public c(int a){
this();
}


C++ code :

class c{
public :
c(){}
c(int a){
this->c(); >>> Error
}
};
Nov 27 '07 #1
3 1339
vinothg
21
Is it because "this" is a reference to the object in java and "this" is a pointer in c++
Nov 27 '07 #2
JosAH
11,448 Expert 8TB
Is it because "this" is a reference to the object in java and "this" is a pointer in c++
In both languages 'this' is a pointer to the object itself. Java doesn't even know
about references (although it (ab)uses the noun in several contexts). C++ has
initialization lists following the colon in the ctor header. Not as elegant as the
Java solution but doable; they're different languages you know.

kind regards,

Jos
Nov 27 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
this->c(); >>> Error
The this pointer can't be used to call a constructor inside the object constructor since this would set up an infinite recursion of calls.

You can, of cource call any constructor inside a constructor provided these calls are not on the same object:
Expand|Select|Wrap|Line Numbers
  1. class c
  2. {
  3.     public :
  4.       c(){}
  5.       c(int a)
  6.       {
  7.                 //this->c(); //>>> Error
  8.     c::c();  //OK. This is a different object
  9.       }
  10. };
  11.  
Also, the function definition inisde the class is a Java thing. In C++ doing that makes the functions inline. Code your member functions outside the class.
Nov 27 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

28
by: Chiller | last post by:
I am in the process of writing a class that will represent metric distances by accepting a value (ie, 3) and a unit of measure (ie, m). I've written my constructor in the .h file as Distance...
6
by: cppaddict | last post by:
Hi, I know that C++ does not have an explicit super() constructor for calling a Base class constructor from a Derived class's constructor, but my understanding is that C++ implements this...
18
by: Matt | last post by:
I try to compare the default constructor in Java and C++. In C++, a default constructor has one of the two meansings 1) a constructor has ZERO parameter Student() { //etc... } 2) a...
31
by: Peter E. Granger | last post by:
I'm fairly new to C++ and VC++, but for the most part it seems to do most of the same things that can be done in Java, with just some syntactic and structural adjustments. However, one thing I...
19
by: Andrew J. Marshall | last post by:
I want to create a class that must receive a parameter when instantiated. In other words, I do not want it to have a "Public Sub New()". 1) Does VB.NET create a default public constructor if I do...
2
by: Nigel Molesworth | last post by:
I'm doing a Java course. Having gone through the use of accessor (setter and getter) methods for instance variables, it goes on to say: "For reasons that cannot be elaborated upon here, it is...
1
by: yancheng.cheok | last post by:
Hi all, According to "How can I handle a constructor that fails?" in http://www.parashift.com/c++-faq-lite/exceptions.html#faq-17.2, whenever there is a constructor fail, we will throw...
11
by: Rahul | last post by:
Hi Everyone, While working with Java, i came across super() which passes values to base class constructor from derived class constructor. I was wondering if this could be implemented in c++ by...
6
by: Christopher | last post by:
Its been awhile and I am rusty. Can the constructor of my class call another method in the same class if that other method does not change any member data? I want to simply have a seperate...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.