LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-04-2006, 07:46 AM   #1
kpachopoulos
Member
 
Registered: Feb 2004
Location: Athens, Greece
Distribution: Gentoo,FreeBSD, Debian
Posts: 705

Rep: Reputation: 30
calling a superclass constructor


Hi,
in the following code segment i get the error, which is marked in comments. What does it mean? I don't seem to find anything relevant

Code:
public class MulticastIncoming extends DatagramIncoming
{
    MulticastSocket msock;
    InetAddress multicastserverIP;    
    	
    public MulticastIncoming() throws SocketException, IOException, UnknownHostException
    {    	
    	super(msock); //CANNOT REFER TO AN INSTANCE FIELD MSOCK, WHILE EXPLICITLY INVOKING A       
                      //CONSTRUCTOR --> ?????? super(new MulticastSocket(PORT)) for example works OK
    	msock=new MulticastSocket(PORT);
    	multicastserverIP=InetAddress.getByName(SERVER);    	
	msock.joinGroup(multicastserverIP);		
    }
    ..........
}
Instead of calling the super() method, the following would compile without any errors. Does it have the same results with the previous constructor? If it is exactly the same, shouldn't the compiler throw some kind of error/warning here too?

Code:
public class MulticastIncoming extends DatagramIncoming
{
    MulticastSocket msock;
    InetAddress multicastserverIP;    
    	
    public MulticastIncoming() throws SocketException, IOException, UnknownHostException
    {    	
 
    	msock=new MulticastSocket(PORT);
    	multicastserverIP=InetAddress.getByName(SERVER);    	
	msock.joinGroup(multicastserverIP);		

        new DatagramIncoming(msock);
    }
    ..........
}

Last edited by kpachopoulos; 09-04-2006 at 07:48 AM.
 
Old 09-04-2006, 12:15 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
The error message is saying that msock is uninitialized before the constructor is run. You can't use variables from the child class before you finish the constructor of the parent.

The second version is slightly different since it calls the default superclass constructor (ie the one without any arguments). Also calling
Code:
new DatagramIncoming(msock);
after the first bit of code won't have any effect: it will create a DatagramIncoming object then discard it.


I'm wondering if you need a seperate MulticastSocket variable in your MulticastIncoming class. If you already have a MulticastSocket variable called msock in the parent DatagramIncoming class then you can reference it from the child class. Assuming the constructor for DatagramIncoming sets the MulticastSocket variable, then you could do this:
Code:
...
public MulticastIncoming() throws SocketException, IOException, UnknownHostException
    {    	
    	super(new MulticastSocket(PORT));

    	multicastserverIP=InetAddress.getByName(SERVER);    	
	msock.joinGroup(multicastserverIP);		
    }
...
Of course I'm just guessing about your DatagramIncoming class.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
not calling copy constructor on function return jhorvath Programming 7 09-22-2009 12:43 PM
No virtual constructor in C++ Why??? krishnacins Programming 10 04-11-2006 09:19 PM
odd recursion: calling "by hand" vs calling by cronscript... prx Programming 4 02-12-2005 04:59 PM
Copy constructor in C++ prenayan Linux - General 3 11-13-2003 02:48 PM
c++ constructor question jhorvath Programming 6 11-06-2003 10:52 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration